Skip to content

Instantly share code, notes, and snippets.

@ransomw
Created February 23, 2023 14:57
Show Gist options
  • Save ransomw/95d9b16e1ad964a878830825bb7a24a1 to your computer and use it in GitHub Desktop.
Save ransomw/95d9b16e1ad964a878830825bb7a24a1 to your computer and use it in GitHub Desktop.
// the `unknown` type is similar to `any`, except it is less permissive on the RHS of assignment
let unknownType: unknown = 'asdf'
let anyType: any = 'asdf';
let someString: string;
// now,
someString = anyType;
// is permitted, while
// someString = unknownType
// errors
// by the way, note that it's possible to have the dynamic and static types
// _of the same variable_ differ because of the permissiveness of the 'any'
// type on the RHS of assignment.
anyType = 1
someString = anyType;
console.log(typeof someString); // 'number'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment