Skip to content

filipjakov/ts-utility-types-analysis

Repository files navigation

ts-utility-types-analysis

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

Types as metavalues

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));

Content

  1. mapped types
  • union and intersection
  • indexed type query and indexed access operator
  • generics
  • mapped types
  1. conditional types
  • conditionals
  • "naked" type parameters
  • never
  1. conditional types with infer
  • conditionals with infering
  1. Bonus
  • advanced custom utility type
  • object type
  • unknown type

About

Reverse engineering Typescript's utility types

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published