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
Implementation
More Implementation: PartialExclude<Type, Keys>
Constructs a type with all properties of Type set to optional except specific Keys.
More Implementation: PartialWith<Type, Keys>
Constructs a type will all properties of Type and set specific Keys to optional.
Readonly<Type>
Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.
Example
Implementation
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.
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
Implementation
More Implementation: MergeRecord<Record, Record>
Pick<Type, Keys>
Constructs a type by picking the set of properties Keys from Type.
Example
Implementation
More Implementation: PickWithTypes<Types, Subtypes>
Constructs a type by picking properties which type in the set of Subtypes from Type.
Omit<Type, Keys>
Constructs a type by picking all properties from Type and then removing Keys.
Example
Implementation
More Implementation: OmitOptional<Type>
Construct a type by omitting all optional properties.
Exclude<Type, ExcludedUnion>
Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.
Example
Implementation
More Implementation: ExcludeKeysInType<Type, Record>
Extract<Type, Union>
Constructs a type bu extracting from Type all union members that are assignable to Union.
Example
Implementation
NonNullable<Type>
Constructs a type by excluding null and undefined from Type.
Example
Implementation
Parameters<Type>
Constructs a tuple type from the types used in the parameters of a function type Type.
Example
Implementation
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
Implementation
ReturnType<Type>
Constructs a type consisting of the return type of function Type.
Constructs a type consisting of the instance type of a constructor function in Type.
Example
Implementation
Required<Type>
Constructs a type consisting of all properties of T set to required. The opposite of Partial.
Example
Implementation
More Implementation: RequiredProps<Type, RequiredUnion>
ThisParameterType<Type>
Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.
Example
Implementation
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
Implementation
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
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.
type PartialExclude<T, M extends keyof T> = {
[ P in keyof T ]?: T[ P ];
} & {
[ P in keyof M ]: T[ P ];
}
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 ];
}
type Options = {
id: number;
type: string;
}
// type ReadonlyOptions = { readonly id: number; readonly type: string; }
type ReadonlyOptions = Readonly<Options>;
type Readonly<T> = {
readonly [ P in keyof T ]: T[ P ];
}
type Unreadonly<T> = {
-readonly [ P in keyof T ]: T[ P ];
}
// ModifiableOptions = { id: number; name: string }
type ModifiableOptions = Unreadonly<{
readonly id: number;
readonly name: string;
}>
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const nav: Record<Page, PageInfo> = {
about : { title : 'about' },
contact : { title : 'contact' },
home : { title : 'home' }
}
type Record<K, T> = {
[ P in K ]: T;
}
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>>;
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>;
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 ];
}
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;
}>
// type T0 = 'b' | 'c'
type T0 = Exclude<'a' | 'b' | 'c', 'a'>
// type T1 = 'c'
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>
type Exclude<T, U> = T extends U ? never : T;
type ExcludeKeysInType<T, U> = Exclude<T, keyof U>;
// type T0 = "age" | "sex"
type T0 = ExcludeKeysInType<'name' | 'age' | 'sex', {
name: string;
}>
// type T0 = 'a'
type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>
type Extract<T, U> = T extends U ? T : never;
// type T0 = string | number
type T0 = NonNullable<string | number | undefined>;
type NonNullable<T> = T extends undefined | null ? never : T;
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>;
type Prameters<T extends ( ...args: any[] ) => any> = T extends ( ...args: infer P ) => any ? P : never;
class Person {
constructor( public name: string, age: number ) {
}
}
// type X = [name: string, age: number]
type X = ConstructorParameters<typeof Person>
type ConstructorParameters<T extends new ( ...args: any[] ) => any> = T extends new ( ...args: infer U ) => any ? U : never;
declare function f1(): { a: number; b: string };
// type T0 = {
// a: number;
// b: string;
// }
type T0 = ReturnType<typeof f1>
type ReturnType<T extends ( ...args: any[] ) => any> = T extends ( ...args: any[] ) => infer U ? U : never;
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>;
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>
type InstanceType<T extends new ( ...args: any ) => any> = T extends new ( ...args: any ) => infer U ? U : any;
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 };
type Required<T> = {
[ P in keyof T ]-?: T[ P ];
}
type RequiredProps<T, U extends keyof any> = {
}
function toHex( this: Number ) {
return this.toString( 16 );
}
function numberToString( n: ThisParameterType<typeof toHex> ) {
return toHex.apply( n );
}
type ThisParameterType<T> = T extends ( this: infer U, ...args: any[] ) => any ? U : unknown;