Created
August 21, 2019 01:48
-
-
Save devdoomari3/2ae56f8343ab6fe9769de72a98f3beac to your computer and use it in GitHub Desktop.
pack_unpack_generics.ts
This file contains hidden or 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
export type PackedGenerics<A,B,C> = { | |
a: A, | |
b: B, | |
c: C | |
} | |
type testType = PackedGenerics<number, boolean, string> | |
type unpack2nd<T extends PackedGenerics<any, any, any>> = T extends PackedGenerics<infer A2, infer B2, infer C2> ? { | |
a2: A2, | |
b2: B2, | |
c2: C2, | |
} : never; | |
type unpacked2nd = unpack2nd<testType> | |
type wrongType = unpack2nd<number> | |
const someWrongValue: wrongType = null as any | |
const a: unpacked2nd = { | |
a2: 3, | |
b2: false, | |
c2: 'a', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment