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
| /** @deprecated use User class instead */ | |
| class OldUser {} | |
| class User { | |
| /** @deprecated */ | |
| constructor(public username: string, public age: number) {} | |
| /** @deprecated */ | |
| static createUser(username: string, age: number) { |
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
| class User { | |
| username: string; | |
| age: number; | |
| constructor(username: string,age: number) { | |
| this.username = username; | |
| this.age = age; | |
| } | |
| static [propName: string]: string | number; |
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
| class User { | |
| username: string; | |
| age: number; | |
| constructor(username: string,age: number) { | |
| this.username = username; | |
| this.age = age; | |
| } | |
| [propName: string]: string | number; |
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
| function userCount() { | |
| /* Mock data */ | |
| return 15; | |
| }; | |
| class User { | |
| id = 0; | |
| static count: number = 0; | |
| constructor( |
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
| { | |
| "compilerOptions": { | |
| "noImplicitOverride": true | |
| } | |
| } |
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
| class Employee { | |
| doWork() { | |
| console.log('I\'m working!'); | |
| } | |
| } | |
| class Developer extends Employee { | |
| override doWork() { | |
| console.log('I\'m programming!') | |
| } |
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 roles = ['read', 'write', 'readAndWrite'] as const; | |
| type Roles = typeof roles[number]; | |
| // equals to this | |
| // type Roles = "read" | "write" | "readAndWrite" | |
| // or even like this | |
| type RolesInCapital = Capitalize<typeof roles[number]>; | |
| // equals to this | |
| // type RolesInCapital = "Read" | "Write" | "ReadAndWrite" |
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 email = 'john@gmail.com'; // type 'john@gmail.com' | |
| const phones = [8494922901, 1238399293]; // type number[] | |
| const session = { id: 123, name: 'x1keck323jKJdf1' }; | |
| // type | |
| // { | |
| // id: number; | |
| // name: string; | |
| // } | |
| const username = 'John' as const; // type 'John' |
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
| function valueCheck(value: string | number) { | |
| if (typeof value === "string") { | |
| // value has type string | |
| } else if (typeof value === "number") { | |
| // value has type number | |
| } else { | |
| value; // value has type never | |
| } | |
| } |
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
| function endlessWhile(): never { | |
| while(true) { | |
| /* ... */ | |
| } | |
| } |
NewerOlder