Skip to content

Instantly share code, notes, and snippets.

@sketchbuch
Last active January 24, 2020 07:04
Show Gist options
  • Save sketchbuch/bd5d6616c9274d742f45bf43e78ddcdc to your computer and use it in GitHub Desktop.
Save sketchbuch/bd5d6616c9274d742f45bf43e78ddcdc to your computer and use it in GitHub Desktop.
TS - Typescript example of a Predicate Type Guard
export interfac-e NpmContributors {
email?: string;
githubUsername?: string;
name: string;
url?: string;
}
export interface NpmMaintainer {
email: string;
name: string;
}
export const isContributor = (npmType: NpmMaintainer | NpmContributors): npmType is NpmContributors => {
return (npmType as NpmContributors).url !== undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment