Created
December 2, 2020 12:50
-
-
Save anthowave/254f8efd2a6025c201cf9d7f35771b30 to your computer and use it in GitHub Desktop.
TS - determine if var is type of custom type
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
const stringLitArray = <L extends string>(arr: L[]) => arr; | |
const linkTypes = stringLitArray(['a', 'link']); | |
export type LinkType = (typeof linkTypes)[number]; | |
const isLinkType = (x: any): x is LinkType => linkTypes.includes(x); | |
let link = 'a'; | |
if (isLinkType('a')) { | |
console.log("The current link is of type 'LinkType'"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More example; How to implement a generic ValueOf<T> helper type in TypeScript.