Analysing and reverse engineering Typescript's utility types:
- Partial - mapped type
- Readonly - mapped type
- Record<K,T> - mapped type
- Pick<T,K> - mapped type
- Omit<T,K> - conditional type (pick + exclude)
- Exclude<T,U> - conditional type
- Extract<T,U> - conditional type
- NonNullable - conditional type
- Parameters - conditional + infer
- ConstructorParameters - conditional + infer
- ReturnType - conditional + infer
- InstanceType - conditional + infer
- Required - mapped type
- ThisParameterType - conditional + infer
- OmitThisParameter - conditional + infer
- ThisType - ???
Source: https://github.com/microsoft/TypeScript/blob/master/lib/lib.es5.d.ts
Consider the following two levels of TypeScript code:
- Program level: At runtime, we can use values and functions.
- Type level: At compile time, we can use specific types and generic types.
The type level is a metalevel of the program level.
Level | Available at | Operands | Operations |
---|---|---|---|
Program | Runtime | Values | Functions |
Type | Compile time | Specific types | Generic types |
type ObjectLiteralType = {
first: 1,
second: 2,
};
type Result = keyof ObjectLiteralType; // (A)
In line A, we are taking the following steps:
- The input of our computation is the type ObjectLiteralType, an object literal type.
- We apply the operation keyof to the input. It lists the property keys of an object type.
- We give the output of keyof the name Result.
const ObjectLiteralType = {
first: 1,
second: 2,
};
console.log(Object.keys(ObjectLiteralType));
- mapped types
- union and intersection
- indexed type query and indexed access operator
- generics
- mapped types
- conditional types
- conditionals
- "naked" type parameters
- never
- conditional types with infer
- conditionals with infering
- Bonus
- advanced custom utility type
- object type
- unknown type