Created
November 2, 2021 17:31
-
-
Save elado/420afd3f3a57483511e84a1111dc1bf2 to your computer and use it in GitHub Desktop.
TypeScript type maps
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
export enum EnumType { | |
Foo = 'Foo', | |
Bar = 'Bar', | |
} | |
export interface MapEnumTypeToDataType { | |
[EnumType.Foo]: { x: string }; | |
[EnumType.Bar]: { y: string }; | |
} | |
function fn<TEnumType extends EnumType>(name: TEnumType, data: MapEnumTypeToDataType[TEnumType]) { | |
console.log(name, data); | |
} | |
export interface MapStringTypeToDataType { | |
"foo": { x: string }; | |
"bar": { y: string }; | |
} | |
function fn2<TStringType extends keyof MapStringTypeToDataType>(name: TStringType, data: MapStringTypeToDataType[TStringType]) { | |
console.log(name, data); | |
} | |
fn(EnumType.Foo, { x: 'foo' }); | |
fn(EnumType.Bar, { y: 'bar' }); | |
fn2("foo", { x: 'foo' }); | |
fn2("bar", { y: 'bar' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment