Skip to content

Instantly share code, notes, and snippets.

@anthowave
Created December 2, 2020 12:50
Show Gist options
  • Save anthowave/254f8efd2a6025c201cf9d7f35771b30 to your computer and use it in GitHub Desktop.
Save anthowave/254f8efd2a6025c201cf9d7f35771b30 to your computer and use it in GitHub Desktop.
TS - determine if var is type of custom type
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'");
}
@anthowave
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment