Skip to content

Instantly share code, notes, and snippets.

@lafiosca
Created January 16, 2020 22:01
Show Gist options
  • Save lafiosca/5d666d34f5b4eeeb617b740a016b6605 to your computer and use it in GitHub Desktop.
Save lafiosca/5d666d34f5b4eeeb617b740a016b6605 to your computer and use it in GitHub Desktop.
Type inference with any versus unknown
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