Skip to content

Instantly share code, notes, and snippets.

@devdoomari3
Created August 21, 2019 01:48
Show Gist options
  • Save devdoomari3/2ae56f8343ab6fe9769de72a98f3beac to your computer and use it in GitHub Desktop.
Save devdoomari3/2ae56f8343ab6fe9769de72a98f3beac to your computer and use it in GitHub Desktop.
pack_unpack_generics.ts
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