Last active
December 6, 2022 17:03
-
-
Save Phryxia/dadc8ab20d3e08a31d8e04591a4266c2 to your computer and use it in GitHub Desktop.
Some useful type manipulations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is also nice type manipulation library - utility-types.