1interface User {2 id: number;3 name: string;4 email: string;5}6// all three fields are requiredtype Patch = Partial<User>;let p: Partial<User> = { id: 7 };type Locked = Readonly<User>;type Tag = Pick<User, "id" | "name">;type NewUser = Omit<User, "id">;type Bad = Pick<User, "age">;type O = { [P in keyof User]?: User[P] };<T>PartialReadonlyPickOmitutility!✓numberidstringnamestringemailnumberstringstringidnameemailUser{ id: 7 }"age"P
Back row: user.ts (the code) and the utility type (the machine)Front row: the fields of User going in, the slots of the new type coming outTokens: each field passing through, and the key P a mapped type visitsAt the back: user.ts, with the User interface and one line that uses a utility type, and the utility type itself. In front: the three fields of User going in, and the slots of the new type coming out. Partial adds a ? to every field, Readonly locks every field, Pick keeps the keys you name and Omit drops them. A key User does not have is caught by tsc. Partial, Readonly, Pick and Omit are declared in lib.es5.d.ts, the type library tsc loads by default. They exist only while checking: like every type, they are erased and add nothing to the JavaScript.

Types from types

  1. Start with one type. User describes an object with three fields: id is a number, name and email are strings. No field has a ? after its name, so all three are required.

Change every field

Choose the fields

How it works

What happens

;; step 1/9 · shape
  1  interface User {
> 2    id: number;
> 3    name: string;
> 4    email: string;
  5  }
  6  // all three fields are required

keyof User  =  "id" | "name" | "email"
a User value must have all three fields

Partial, Readonly, Pick and Omit are declared in lib.es5.d.ts, the type library tsc loads by default. They exist only while checking: like every type, they are erased and add nothing to the JavaScript. Omit does not check its keys: it is declared as Omit<T, K extends keyof any>, so Omit<User, "age"> is allowed and simply removes nothing. Pick requires K extends keyof T, which is why the typo is caught.