Skip to content

Instantly share code, notes, and snippets.

@tswistak
Created July 30, 2018 06:14
Show Gist options
  • Save tswistak/830bebc1cc2ab768edc887f5d7cf7d81 to your computer and use it in GitHub Desktop.
Save tswistak/830bebc1cc2ab768edc887f5d7cf7d81 to your computer and use it in GitHub Desktop.
TypeScript 3.0, listing 7
const a1: any = 'a';
const a2: unknown = 'a';
a1.length; // = 1
a2.length; // compiler error
a1(); // error during execution
a2(); // compiler error
new a1(); // error during execution
new a2(); // compiler error
const a3 = a1 + 3; // = 'a3'
const a4 = a2 + 3; // compiler error
const b: unknown = { a: 'b' };
console.log(b.a); // compiler error
function hasA(obj: any): obj is { a: any } {
return !!obj
&& typeof obj === 'object'
&& 'a' in obj;
}
if (hasA(b)) {
console.log(b.a); // = 'b'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment