TypeScript Type Manipulation

오픈소스·2022년 12월 3일
0
post-thumbnail
post-custom-banner

https://www.typescriptlang.org/docs/handbook/2/types-from-types.html

  • Generics - Types which take parameters
  • Keyof Type Operator - Using the keyof operator to create new types
  • Typeof Type Operator - Using the typeof operator to create new types
  • Indexed Access Types - Using Type['a'] syntax to access a subset of a type
  • Conditional Types - Types which act like if statements in the type system
  • Mapped Types - Creating types by mapping each property in an existing type
  • Template Literal Types - Mapped types which change properties via template literal strings

https://github.com/microsoft/TypeScript/blob/main/lib/lib.es5.d.ts

...

/**
 * Make all properties in T optional
 */
type Partial<T> = {
    [P in keyof T]?: T[P];
};

/**
 * Make all properties in T required
 */
type Required<T> = {
    [P in keyof T]-?: T[P];
};

/**
 * Make all properties in T readonly
 */
type Readonly<T> = {
    readonly [P in keyof T]: T[P];
};

/**
 * From T, pick a set of properties whose keys are in the union K
 */
type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

/**
 * Construct a type with a set of properties K of type T
 */
type Record<K extends keyof any, T> = {
    [P in K]: T;
};

/**
 * Exclude from T those types that are assignable to U
 */
type Exclude<T, U> = T extends U ? never : T;

/**
 * Extract from T those types that are assignable to U
 */
type Extract<T, U> = T extends U ? T : never;

/**
 * Construct a type with the properties of T except for those in type K.
 */
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

/**
 * Exclude null and undefined from T
 */
type NonNullable<T> = T & {};

...
post-custom-banner

0개의 댓글