Skip to content

Instantly share code, notes, and snippets.

@Phryxia
Last active December 6, 2022 17:03
Show Gist options
  • Save Phryxia/dadc8ab20d3e08a31d8e04591a4266c2 to your computer and use it in GitHub Desktop.
Save Phryxia/dadc8ab20d3e08a31d8e04591a4266c2 to your computer and use it in GitHub Desktop.
Some useful type manipulations
// why [A]? check https://github.com/microsoft/TypeScript/issues/31751
type Equal<A, B> =
[A] extends [B] ?
[B] extends [A] ?
true :
false :
false
// [A, ...?] -> A
type First<T> =
T extends [infer R, ...unknown[]] ?
R :
T extends (infer R)[] ?
R :
never
// [?, ...A] -> A
type Rest<T> =
T extends [unknown, ...infer Rest] ?
Rest :
T extends [] ?
never :
T extends (infer R)[] ?
T :
never
@Phryxia
Copy link
Author

Phryxia commented Dec 6, 2022

There is also nice type manipulation library - utility-types.

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