Notes de Jérôme Prudent
Notes publiques ...

« Notes

Typescript utility types

Créée le 02 Mar 2026 — mise à jour le 04 Sep 2026

doc-review typescript

  • https://www.typescriptlang.org/docs/handbook/utility-types.html
  • #doc-review #typescript
  • Utility types are generic types that transform other types
  • Awaited<Type>
    • type A = Awaited<Promise<string>>;  
      //type A = string
      
      type B = Awaited<Promise<Promise<number>>>;
      //type B = number
      
      type C = Awaited<boolean | Promise<number>>;
      //type C = number | boolean
      
    • nice to “flatmap” deeply nested Promise
  • Partial<Type>
    • interface Todo {
        title: string;
        description: string;
      }
      
      function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
        return { ...todo, ...fieldsToUpdate };
      }
      
    • set all fields optional (is it equivalent to undefined ?)
    • represent all subset of a given type
  • Required<Type>
  • Readonly<Type>
    • like Partial but set readonly
    • fails at runtime (why not at compile ???)
  • ReadonlyArray<Type>
  • Record<Keys, Type>
    • interface CatInfo {
        age: number;
        breed: string;
      }
      
      const cats: Record<CatName, CatInfo> = {
        miffy: { age: 10, breed: "Persian" },
        boris: { age: 5, breed: "Maine Coon" },
        mordred: { age: 16, breed: "British Shorthair" },
      };
      
    • like a typed Map
  • Pick<Type, Keys>
    • interface Todo {
        title: string;
        description: string;
        completed: boolean;
      }
      
      type TodoPreview = Pick<Todo, "title" | "completed">;
      
      const todo: TodoPreview = {
        title: "Clean room",
        completed: false,
      };
      
    • construct a subset of a type
    • see also Partial
  • Omit<Type, Keys>
  • Exclude<UnionType, ExcludedMembers>
    • exclude a type from a union type
  • Extract<Type, Union>
  • NonNullable<Type>
  • Parameters<Type>
  • ConstructorParameters<Type>
  • ReturnType<Type>
  • InstanceType<Type>
  • NoInfer<Type>
  • ThisParameterType<Type>
  • OmitThisParameter<Type>
  • ThisType<Type>