Typescript Types
Typescript is Javascript with type safety.
Generics
When a function or type has more than one generic type parameter, there's usually a constraint involved.
Type Predicate
A Predicate function is any function that returns a boolean. Type predicates are predicate functions that also change their arguments' static types.
TypeScript trusts us to implement type predicates correctly. Test your predicate functions well to make sure that they do what you think they do.
Type predicates can take unions as their arguments, or they can take unknown. In most cases, unknown is the safest and most flexible option
Type Narrowing
https://dev.to/smeijer/typescript-type-guards-and-type-predicates-4m5e
Type narrowing is a common challenge in TypeScript, and we solve it with type guards. We've now seen a few different kinds of type guards:
Directly checking the type with the typeof operator, like typeof aValue === 'number'. Comparing against a value, like aValue !== undefined. Type predicate functions like the built-in Array.isArray(), or the isAddress that we wrote above.
Type narrowing lets us write separate code to handle union alternatives. For example, if we have a number | undefined variable, we can write separate code to handle the number case vs. the undefined case. Type guards are special expressions used inside of if conditions, like if (Array.isArray(...)) or if (typeof n === 'number'). We use them to narrow types. They also work inside switch statements and ternary expressions like typeof x === 'number' ? x : y. Type predicates let us write our own functions that act as type guards. TypeScript comes with some type predicates predefined, like Array.isArray, but we can define our own as well
Infer Types
Only works inside the condition of a conditional type, and it can only infer generic type parameters.
type ArrayContents<T> = T extends Array<infer ElementType> ? ElementType : never;