Sobes.tech
Middle

Какие utility-типы в TypeScript ты использовала? Знакома ли с ReturnType?

sobes.tech AI

Answer from AI

In TypeScript, there are several useful utility types that help work with types more flexibly:

  • Partial<T> — makes all properties of type T optional.
  • Required<T> — makes all properties mandatory.
  • Readonly<T> — makes all properties read-only.
  • Pick<T, K> — selects a subset of properties from type T.
  • Omit<T, K> — excludes specified properties from type T.
  • Record<K, T> — creates an object type with keys K and values T.
  • ReturnType<T> — extracts the return type of function T.

Example of using ReturnType:

 type Fn = (a: number, b: string) => boolean;
 type Result = ReturnType<Fn>; // boolean

ReturnType is useful when you need to get the result type of a function without duplicating the type definition.