Curriculum
Course: TypeScript
Login
Text lesson

Exclude

Exclude removes specified types from a union type.

Example

type Primitive = string | number | boolean
const value: Exclude<Primitive, string> = true// a string cannot be used here since Exclude removed it from the type.

ReturnType

ReturnType extracts the return type of a function type.

Example

type PointGenerator = () => { x: number; y: number; };
const point: ReturnType<PointGenerator> = {
  x: 10,
  y: 20
};

Parameters

Parameters extracts the parameter types of a function type as a tuple.

Example

type PointPrinter = (p: { x: number; y: number; }) => void;
const point: Parameters<PointPrinter>[0] = {
  x: 10,
  y: 20
};

Readonly

Readonly creates a new type where all properties are read-only, meaning they cannot be modified after being assigned a value.

Keep in mind that TypeScript will prevent modifications to readonly properties at compile time. However, since TypeScript compiles to JavaScript, it is still theoretically possible to override a readonly property in runtime JavaScript.

Example

interface Person {
  name: string;
  age: number;
}
const person: Readonly<Person> = {
  name: “Dylan”,
  age: 35,
};
person.name = ‘Israel’// prog.ts(11,8): error TS2540: Cannot assign to ‘name’ because it is a read-only property.