Skip to content

Instantly share code, notes, and snippets.

@wzhudev
Last active July 27, 2021 14:56
Show Gist options
  • Save wzhudev/fdf122eb44433957f5e2b6fdd61e3693 to your computer and use it in GitHub Desktop.
Save wzhudev/fdf122eb44433957f5e2b6fdd61e3693 to your computer and use it in GitHub Desktop.

Solutions

Warm-up

Easy

  1. Pick
type MyPick<T, K extends keyof T> = {
  [U in K]: T[U];
}
  1. Readonly
type MyReadonly<T> = {
  readonly [U in keyof T]: T[U];
}
  1. First of Array
type First<T extends any[]> = T['length'] extends 0 ? never : T[0]; 

Perhaps you don't know you can get length of an array type.

  1. Lengh of Tuple
type Length<T extends readonly unknown[]> = T['length'];
  1. Exclude
type MyExclude<T, U> = T extends U ? never : T;

Actually, it runs for each sub type of a union type.

  1. Awaited
type Awaited<T extends Promise<any>> = T extends Promise<infer U> ? U : never;
  1. Contact
type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U];

Medium

Hard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment