Last active
September 23, 2021 05:08
-
-
Save phoenisx/54a213bbae1f9cff7acff3f31cd1a05a to your computer and use it in GitHub Desktop.
Advance Typescript snippets for reference
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
/** | |
* Following is a selector that helps to dynamically create a type from existing constants | |
* This can be really helpful to create type patterns which works flawlessly with JS dynamic behaviour | |
* and reduce the amount of code required to add specific code for each type separately | |
* | |
* Ref: https://www.typescriptlang.org/docs/handbook/2/mapped-types.html | |
*/ | |
export enum SUPPORTED_HEROES { | |
SPIDER = "spiderman", | |
SUPER = "superman", | |
IRON = "ironman", | |
} | |
type Selector = { | |
-readonly [key in keyof typeof SUPPORTED_HEROES as key extends string | |
? `local${Capitalize<typeof SUPPORTED_HEROES[key]>}State` | |
: never]: string; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment