Created
July 30, 2018 06:14
-
-
Save tswistak/830bebc1cc2ab768edc887f5d7cf7d81 to your computer and use it in GitHub Desktop.
TypeScript 3.0, listing 7
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
| 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