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

Required<Type>

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

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

Readonly<Type>

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

Example
  1. interface Todo {
    title: string;
    }
  2. const todo: Readonly<Todo> = {
    title: "Delete inactive users",
    };
  3. todo.title = "Hello";
    Cannot assign to 'title' because it is a read-only property.2540Cannot assign to 'title' because it is a read-only property.

This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a ).

Object.freeze
  1. ts
    function freeze<Type>(obj: Type): Readonly<Type>;

Record<Keys,Type>

Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.

Example
  1. interface CatInfo {
    age: number;
    breed: string;
    }
  2. type CatName = "miffy" | "boris" | "mordred";
  3. const cats: Record<CatName, CatInfo> = {
    miffy: { age: 10, breed: "Persian" },
    boris: { age: 5, breed: "Maine Coon" },
    mordred: { age: 16, breed: "British Shorthair" }
    };
  4. cats.boris;
    // ^ = const cats: Record

Pick<Type, Keys>

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

Example
Example
  1. interface Todo {
    title: string;
    description: string;
    completed: boolean;
    }
  2. type TodoPreview = Omit<Todo, "description">;
  3. const todo: TodoPreview = {
    title: "Clean room",
    completed: false,
    };
  4. todo;
    // ^ = const todo: TodoPreview

Exclude<Type, ExcludedUnion>

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

Example
  1. type T0 = Exclude<"a" | "b" | "c", "a">;
    // ^ = type T0 = "b" | "c"
  2. type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
    // ^ = type T1 = "c"
  3. type T2 = Exclude<string | number | (() => void), Function>;
    // ^ = type T2 = string | number

Extract<Type, Union>

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

Example
  1. type T0 = Extract<"a" | "b" | "c", "a" | "f">;
    // ^ = type T0 = "a"
  2. type T1 = Extract<string | number | (() => void), Function>;
    // ^ = type T1 = () => void

NonNullable<Type>

Constructs a type by excluding null and undefined from Type.

Example
  1. type T0 = NonNullable<string | number | undefined>;
    // ^ = type T0 = string | number
  2. type T1 = NonNullable<string[] | null | undefined>;
    // ^ = type T1 = string[]

Parameters<Type>

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

Example

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
  1. type T0 = ConstructorParameters<ErrorConstructor>;
    // ^ = type T0 = [message?: string]
  2. type T1 = ConstructorParameters<FunctionConstructor>;
    // ^ = type T1 = string[]
  3. type T2 = ConstructorParameters<RegExpConstructor>;
    // ^ = type T2 = [pattern: string | RegExp, flags?: string]
  4. type T3 = ConstructorParameters<any>;
    // ^ = type T3 = unknown[]
  5. Type 'Function' provides no match for the signature 'new (...args: any): any'.2344Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
  6. Type 'Function' provides no match for the signature 'new (...args: any): any'.// ^ = type T4 = never

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

Example
  1. declare function f1(): { a: number; b: string };
  2. type T0 = ReturnType<() => string>;
    // ^ = type T0 = string
  3. type T1 = ReturnType<(s: string) => void>;
    // ^ = type T1 = void
  4. type T2 = ReturnType<<T>() => T>;
    // ^ = type T2 = unknown
  5. type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
    // ^ = type T3 = number[]
  6. type T4 = ReturnType<typeof f1>;
    // ^ = type T4 = {
  7. // a: number;
  8. // b: string;
  9. type T5 = ReturnType<any>;
    // ^ = type T5 = any
  10. type T6 = ReturnType<never>;
    // ^ = type T6 = never
  11. type T7 = ReturnType<string>;
    Type 'string' does not satisfy the constraint '(...args: any) => any'.2344Type 'string' does not satisfy the constraint '(...args: any) => any'.// ^ = type T7 = any
  12. type T8 = ReturnType<Function>;
    Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  13. Type 'Function' provides no match for the signature '(...args: any): any'.2344Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  14. Type 'Function' provides no match for the signature '(...args: any): any'.// ^ = type T8 = any

InstanceType<Type>

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

ThisParameterType<Type>

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

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

OmitThisParameter<Type>

Removes the this parameter from Type. If Type has no explicitly 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

ThisType<Type>

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

Example
  1. type ObjectDescriptor<D, M> = {
    data?: D;
    methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
    };
  2. 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;
    }
  3. let obj = makeObject({
    data: { x: 0, y: 0 },
    methods: {
    moveBy(dx: number, dy: number) {
    this.x += dx; // Strongly typed this
    this.y += dy; // Strongly typed this
    },
    },
    });

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 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 inference 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.

Intrinsic String Manipulation Types

To help with string manipulation around template string literals, TypeScript includes a set of types which can be used in string manipulation within the type system. You can find those in the documentation.