TypeScript - Type detection
When dealing with unions and intersections of different types, you will often need to handle each of the cases with some adjustments inside your code. TypeScript is going to take care not to miss any edge cases.
To handle each type separately, you need to know how to detect specific types. You need to perform the check so the TypeScript will be able to detect narrowed variable type inside the branch.
Basic types narrowing
I know how to narrow basic types like string, number, and boolean using the typeof check
I know that to detect null and undefined variables, all I need is ===
Array detection
I know that I can use the Array.isArray utility to narrow down the type to an array
Object detection
I know that I can use typeof operator to check for object but I'm aware that I need an additional check for null value
Different object types
I know that object type detection is a more complex operation. I understand that checking for the existence of keys with the in operator or their value may not be sufficient.
I also know about the type guard technique that helps with code expecting multiple different types of objects.
Class detection
I know that I can use instanceof operator to check if variable is a specific class type. I know how inheritance affects results of this check
I know how to narrow basic types like string, number, and boolean using the typeof check
I know that to detect null and undefined variables, all I need is ===
I know that I can use the Array.isArray utility to narrow down the type to an array
I know that I can use typeof operator to check for object but I'm aware that I need an additional check for null value
I know that object type detection is a more complex operation. I understand that checking for the existence of keys with the in operator or their value may not be sufficient.
I also know about the type guard technique that helps with code expecting multiple different types of objects.
I know that I can use instanceof operator to check if variable is a specific class type. I know how inheritance affects results of this check