Skip to content

Instantly share code, notes, and snippets.

@rcdexta
Last active December 31, 2019 06:49
Show Gist options
  • Select an option

  • Save rcdexta/c26aa24edc4f95ab89973993bbb1c697 to your computer and use it in GitHub Desktop.

Select an option

Save rcdexta/c26aa24edc4f95ab89973993bbb1c697 to your computer and use it in GitHub Desktop.
function isDate(input: unknown) : asserts input is Date {
if (input instanceof Date)
return;
else
throw new Error('Input must be a Date!');
}
function getYear(input: unknown) : number {
isDate(input);
return input.getFullYear() // TypeScripts knows that input is Date
}
console.log(getYear(new Date('2019-01-01')));
console.log(getYear('2019-01-01'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment