Skip to content

Instantly share code, notes, and snippets.

@phoenisx
Last active September 23, 2021 05:08
Show Gist options
  • Save phoenisx/54a213bbae1f9cff7acff3f31cd1a05a to your computer and use it in GitHub Desktop.
Save phoenisx/54a213bbae1f9cff7acff3f31cd1a05a to your computer and use it in GitHub Desktop.
Advance Typescript snippets for reference
/**
* 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