Partial<Type>

Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets of a given type.

Example

type Options = { id: number; type: string; } // OptionalOptions = { id?: number; type?: string; } type OptionalOptions = Partial<Options>;

Implementation

type Partial<T> = { [ P in keyof T ]?: T[ P ]; }

More Implementation: PartialExclude<Type, Keys>

Constructs a type with all properties of Type set to optional except specific Keys.
type PartialExclude<T, M extends keyof T> = { [ P in keyof T ]?: T[ P ]; } & { [ P in keyof M ]: T[ P ]; }

More Implementation: PartialWith<Type, Keys>

Constructs a type will all properties of Type and set specific Keys to optional.
type Diff<T, U> = T extends U ? never : T; type PartialWith<T, M extends keyof T> = { [ P in Diff<keyof T, M> ]: T[ P ]; } & { [ P in M ]?: T[ P ]; }
type PartialWith<T, M extends keyof T> = { [ K in { [ P in keyof T ]: P extends M ? never : P }[ keyof T ] ]: T[ K ]; } & { [ P in M ]?: T[ P ]; }

Readonly<Type>

Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.

Example

type Options = { id: number; type: string; } // type ReadonlyOptions = { readonly id: number; readonly type: string; } type ReadonlyOptions = Readonly<Options>;

Implementation

type Readonly<T> = { readonly [ P in keyof T ]: T[ P ]; }

More Implementation: Unreadonly<Type>

Constructs a type with all properties of Type remove readonly modifier, meaning the properties of the constructed type are allowed to be reassigned.
type Unreadonly<T> = { -readonly [ P in keyof T ]: T[ P ]; } // ModifiableOptions = { id: number; name: string } type ModifiableOptions = Unreadonly<{ readonly id: number; readonly name: string; }>

Record<Keys, Type>

Constructs a type with a set of properties Keys of type Type. This utility can be used to map the properties of a type to another type.

Example

interface PageInfo { title: string; } type Page = 'home' | 'about' | 'contact'; const nav: Record<Page, PageInfo> = { about : { title : 'about' }, contact : { title : 'contact' }, home : { title : 'home' } }

Implementation

type Record<K, T> = { [ P in K ]: T; }

More Implementation: MergeRecord<Record, Record>

type MergeRecord<M, N> = { [ P in keyof M | keyof N ]: M[ keyof M ] | N[ keyof N ]; } // Options = { // [x: string]: number | boolean; // [x: number]: number | boolean; // } type Options = MergeRecord<Record<string, number>, Record<number, boolean>>;

Pick<Type, Keys>

Constructs a type by picking the set of properties Keys from Type.

Example

interface Todo { title: string; description: string; compleled: boolean; } type TodoPreview = Pick<Todo, 'title' | 'completed'>; const todo: TodoPreview = { title: 'Clean room', completed: false }

Implementation

type Pick<T, K> = { [ P in K ]: T[ P ]; }

More Implementation: PickWithTypes<Types, Subtypes>

Constructs a type by picking properties which type in the set of Subtypes from Type.
type PickWithTypes<T, S> = { [ K in { [ P in keyof T ]: S extends T[ P ] ? P : never; }[ keyof T ] ]: T[ K ]; } type SomeTypes = { name: string; age: number; sex: string; married: boolean; } // type A = { // age: number; // } type A = PickWithTypes<SomeTypes, number>; // type B = { // name: string; // sex: string; // } type B = PickWithTypes<SomeTypes, string>; // type C = { // name: string; // sex: string; // married: boolean; // } type C = PickWithTypes<SomeTypes, string | boolean>;

Omit<Type, Keys>

Constructs a type by picking all properties from Type and then removing Keys.

Example

interface Todo { title: string; describe: string; completed: boolean; } type TodoPreview = Omit<Todo, 'description'>; const todo: TodoPreview = { title: 'Clean room', completed: false };

Implementation

type Exclude<T, U> = T extends U ? never : T; type Omit<T, K extends keyof any> = { [ P in Exclude<keyof T, K> ]: T[ P ]; }
type Omit<T, K extends keyof any> = { [ Key in { [ P in keyof T ]: P extends K ? never : P }[ keyof T ] ]: T[ Key ]; }

More Implementation: OmitOptional<Type>

Construct a type by omitting all optional properties.
type OmitOptional<T> = { [ K in { [ P in keyof T ]: undefined extends T[ P ] ? never : P }[ keyof T ] ]: T[ K ]; } // type O = { // name: string; // age: number; // } type O = OmitOptional<{ name: string; age: number; sex?: string; }>

Exclude<Type, ExcludedUnion>

Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.

Example

// type T0 = 'b' | 'c' type T0 = Exclude<'a' | 'b' | 'c', 'a'> // type T1 = 'c' type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>

Implementation

type Exclude<T, U> = T extends U ? never : T;

More Implementation: ExcludeKeysInType<Type, Record>

type ExcludeKeysInType<T, U> = Exclude<T, keyof U>; // type T0 = "age" | "sex" type T0 = ExcludeKeysInType<'name' | 'age' | 'sex', { name: string; }>

Extract<Type, Union>

Constructs a type bu extracting from Type all union members that are assignable to Union.

Example

// type T0 = 'a' type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>

Implementation

type Extract<T, U> = T extends U ? T : never;

NonNullable<Type>

Constructs a type by excluding null and undefined from Type.

Example

// type T0 = string | number type T0 = NonNullable<string | number | undefined>;

Implementation

type NonNullable<T> = T extends undefined | null ? never : T;

Parameters<Type>

Constructs a tuple type from the types used in the parameters of a function type Type.

Example

declare function f1( arg: { a: number; b: string } ): void; // type T0 = [] type T0 = Parameters<() => string>; // type T1 = [s: string] type T1 = Parameters<(s: string) => void>; // type T2 = [arg: unknown] type T2 = Parameters<<T>(arg: T) => T>; // type T3 = [arg: { // a: number; // b: string; // }] type T3 = Parameters<typeof f1>; // type T4 = unknowwn[] type T4 = Parameters<any>; // type T5 = never type T5 = Parameters<never>; // type T6 = never // Type 'string' does not satisfy the constraint '(...args: any) => any'.(2344) type T6 = Parameters<string>; // type T7 = never // Type 'Function' does not satisfy the constraint '(...args: any) => any'. // Type 'Function' provides no match for the signature '(...args: any): any'.(2344) type T7 = Parameters<Function>;

Implementation

type Prameters<T extends ( ...args: any[] ) => any> = T extends ( ...args: infer P ) => any ? P : never;

ConstructorParameters<Type>

Constructs a tuple or array type from the types of a constructor function type. It produces a tuple type with all the parameter types (or the type never if Type is not a function).

Example

class Person { constructor( public name: string, age: number ) { } } // type X = [name: string, age: number] type X = ConstructorParameters<typeof Person>

Implementation

type ConstructorParameters<T extends new ( ...args: any[] ) => any> = T extends new ( ...args: infer U ) => any ? U : never;

ReturnType<Type>

Constructs a type consisting of the return type of function Type.

Example

declare function f1(): { a: number; b: string }; // type T0 = { // a: number; // b: string; // } type T0 = ReturnType<typeof f1>

Implementation

type ReturnType<T extends ( ...args: any[] ) => any> = T extends ( ...args: any[] ) => infer U ? U : never;

Known limitation

function f1( name: string ): string; function f1( name: number ): number; function f1( name: number ): boolean; function f1( name: number ): Date; function f1( name: number ): Promise<any>; function f1( name: number ): () => {}; function f1( name: number ): new () => any; function f1( name: any ): any { return name; } type OverloadedReturnType<T> = T extends { (...args: any[]) : infer R; (...args: any[]) : infer R; (...args: any[]) : infer R ; (...args: any[]) : infer R } ? R : T extends { (...args: any[]) : infer R; (...args: any[]) : infer R; (...args: any[]) : infer R } ? R : T extends { (...args: any[]) : infer R; (...args: any[]) : infer R } ? R : T extends (...args: any[]) => infer R ? R : any // type T1 = new () => any type T1 = ReturnType<typeof f1>; // type T2 = (new () => any) | Date | Promise<any> | (() => {}) type T2 = OverloadedReturnType<typeof f1>;

InstanceType<Type>

Constructs a type consisting of the instance type of a constructor function in Type.

Example

class C { x = 0; y = 0; } // type T0 = C type T0 = InstanceType<typeof C>; // type T1 = any type T1 = InstanceType<any>; // type T2 = never type T2 = InstanceType<never>; // type T3 = any // Type 'string' does not satisfy the constraint 'new (...args: any) => any'.(2344) type T3 = InstanceType<string> // type T4 = any // Type 'Function' does not satisfy the constraint 'new (...args: any) => any'. // Type 'Function' provides no match for the signature 'new (...args: any): any'.(2344) type T4 = InstanceType<Function>

Implementation

type InstanceType<T extends new ( ...args: any ) => any> = T extends new ( ...args: any ) => infer U ? U : any;

Required<Type>

Constructs a type consisting of all properties of T set to required. The opposite of Partial.

Example

interface Props { a?: number; b?: string; } const obj: Props = { a : 5 }; // Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.(2741) const obj2: Required<Props> = { a : 5 };

Implementation

type Required<T> = { [ P in keyof T ]-?: T[ P ]; }

More Implementation: RequiredProps<Type, RequiredUnion>

type RequiredProps<T, U extends keyof any> = { }

ThisParameterType<Type>

Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.

Example

function toHex( this: Number ) { return this.toString( 16 ); } function numberToString( n: ThisParameterType<typeof toHex> ) { return toHex.apply( n ); }

Implementation

type ThisParameterType<T> = T extends ( this: infer U, ...args: any[] ) => any ? U : unknown;

OmitThisParameter<Type>

Removes the this parameter from Type. if Type has no explicity declared this parameter, the result is simply Type. Otherwise, a new function type with no this parameter is created from Type. Generics are erased and only the last overload signature is propagated into the new function type.

Example

function toHex( this: number ) { return this.toString( 16 ); } const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind( 5 ); console.log( fiveToHex() );

Implementation

type OmitThisParameter<T> = T extends ( this: infer U, ...args: infer A ) => infer R ? ( ...args: A ) => R : T;
type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends ( ...args: infer A ) => infer R ? ( ...args: A ) => R : T;

ThisType<Type>

This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type. Note that the --noImplicitThis flag must be enabled to use this utility.

Example

type ObjectDescriptor<D, M> = { data?: D; methods?: M & ThisType<D & M>; }; function makeObject<D, M>( desc: ObjectDescriptor<D, M> ): D & M { let data: object = desc.data || {}; let methods: object = desc.methods || {}; return { ...data, ...methods } as D & M; } let obj = makeObject( { data: { x : 0, y : 0 }, methods : { moveBy( dx: number, dy: number ) { this.x += dx; this.y += dy; } } } );
In the example above, the methods object in the argument to makeObject has a contextual type that includes ThisType<D & M> and therefore the type of this in methods within the methods object is { x: number, y: number } & { moveBy( dx: number, dy: number ): number }.
Notice how the type of the methods property simultaneously is an interface target and a source for the this type in methods.
The ThisType<T> marker interface is simply an empty interface declared in lib.d.ts. Beyond being recognized in the contextual type of an object literal, the interface acts like any empty interface.
💡
If you add & ThisType<WhateverYouWantThisToBe> to the type fo an object, the functions within that object will have WhateverYouWantThisToBe as the type used for this. This is helpful if you can't otherwise specify what this should be through the normal TypeScript mechanisms.
let x: ThisType<String> = { bar() { this.toLowerCase(); } }

Implementation

interface ThisType<T> {}
 
badge