Created
January 16, 2020 22:01
-
-
Save lafiosca/5d666d34f5b4eeeb617b740a016b6605 to your computer and use it in GitHub Desktop.
Type inference with any versus unknown
This file contains 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 foo: any = 42; | |
if (foo !== undefined && typeof foo !== 'string') { | |
throw new Error('foo must be a string if defined'); | |
} | |
console.log(foo); // foo: any | |
const bar: unknown = 42; | |
if (bar !== undefined && typeof bar !== 'string') { | |
throw new Error('bar must be a string if defined'); | |
} | |
console.log(bar); // bar: string | undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment