Last active
December 31, 2019 06:49
-
-
Save rcdexta/c26aa24edc4f95ab89973993bbb1c697 to your computer and use it in GitHub Desktop.
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
| 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