Example 1
ts
Click here for the answer
Yes, this will compile. Unfortunately, obviously NaN is a number in JavaScript.
tsTryletage = 38age =Number .NaN 
Example 2
ts
Click here for the answer
Yes, this will compile. Unfortunately, because tuples are a specialized flavor
of arrays (and at runtime, they actually are just regular arrays) they expose the
entire array API. Look at the type signature of .push()
tsTryconstvector3 : [number, number, number] = [3, 4, 5]vector3 .push (6)
Example 3
ts
Click here for the answer
No, this will not compile.
tsTrytypeDuplicate identifier 'Color'.2300Duplicate identifier 'Color'.= { Color red : numbergreen : numberblue : number}interfaceDuplicate identifier 'Color'.2300Duplicate identifier 'Color'.{ Color alpha : number}
Example 4
ts
Click here for the answer
No, this will not compile.
tsTryclassPerson {name : stringProperty 'friends' has no initializer and is not definitely assigned in the constructor.2564Property 'friends' has no initializer and is not definitely assigned in the constructor.: friends Person []constructor(name : string) {this.name =name }}
Example 5
ts
Click here for the answer
No, this will not compile.
tsTryabstract classPerson {public abstractname : string}classStudent extendsPerson {publicProperty 'name' in type 'Student' is not assignable to the same property in base type 'Person'. Type 'string | string[]' is not assignable to type 'string'. Type 'string[]' is not assignable to type 'string'.2416Property 'name' in type 'Student' is not assignable to the same property in base type 'Person'. Type 'string | string[]' is not assignable to type 'string'. Type 'string[]' is not assignable to type 'string'.: string | string[] = ["Mike North"] name }
Example 6
ts
Click here for the answer
No, this will not compile.
tsTryinterfaceColor {red : numbergreen : numberblue : number}functionprintColor (color :Color ) {// ... //}printColor ({red : 255,green : 0,blue : 0,Object literal may only specify known properties, and 'alpha' does not exist in type 'Color'.2353Object literal may only specify known properties, and 'alpha' does not exist in type 'Color'.: 0.4, alpha })
Example 7
ts
Click here for the answer
Yes, this will compile.
tsTrytypeColor = {red : numbergreen : numberblue : number}classColorValue implementsColor {constructor(publicred : number,publicgreen : number,publicblue : number) {}}
Example 8
ts
Click here for the answer
No, this will NOT compile. When one part of a merged declaration is exported, all other parts must be exported as well.
tsTryexport classIndividual declarations in merged declaration 'Person' must be all exported or all local.2395Individual declarations in merged declaration 'Person' must be all exported or all local.{ Person name : string = ""}interfaceIndividual declarations in merged declaration 'Person' must be all exported or all local.2395Individual declarations in merged declaration 'Person' must be all exported or all local.{ Person age ?: number}
Example 9
ts
Click here for the answer
No, this will NOT compile. The callback passed to .then is not
regarded as a “definite assignment”. In fact, all callbacks are treated this way.
tsTryclassPerson {Property 'name' has no initializer and is not definitely assigned in the constructor.2564Property 'name' has no initializer and is not definitely assigned in the constructor.: string name constructor(userId : string) {// Fetch user's name from an API endpointfetch (`/api/user-info/${userId }`).then ((resp ) =>resp .json ()).then ((info ) => {this.name =info .name // set the user's name})}}
Example 10
ts
Click here for the answer
No, this will NOT compile, but it’s probably more nuanced than you expected!
Once you provide a string initializer for an enum member, all following enum members need an explicit initializer of some sort, unless you go back to numeric enum values, at which point inference takes over again.
Ok, this one wasn’t really fair :)
tsTryenumLanguage {TypeScript = "TS",Enum member must have initializer.1061Enum member must have initializer., JavaScript }enumEditor {SublimeText ,VSCode = "vscode",}enumLinter {ESLint ,TSLint = "tslint",JSLint = 3,JSHint ,}
Example 11
ts
Click here for the answer
No, this will NOT compile. When you have a free-standing function like this,
and refer to the this value, we need to give it a type of some sort.
tsTryfunctionhandleClick (evt :Event ) {const$element =evt .target asHTMLInputElement if ('this' implicitly has type 'any' because it does not have a type annotation.2683'this' implicitly has type 'any' because it does not have a type annotation.this .value !== "") {'this' implicitly has type 'any' because it does not have a type annotation.'this' implicitly has type 'any' because it does not have a type annotation.2683this .value =this .value .toUpperCase ()
2683'this' implicitly has type 'any' because it does not have a type annotation.'this' implicitly has type 'any' because it does not have a type annotation.}}
Here’s a version that would compile
tsTryfunctionhandleClick (this :HTMLInputElement ,evt :Event ) {const$element =evt .target asHTMLInputElement if (this.value !== "") {this.value = this.value .toUpperCase ()}}
Example 12
ts
Click here for the answer
No, this will NOT compile. Because TS private fields are just “checked
for access at build time”
and are totally accessible outside the class at runtime, there’s a collision
between the two age members.
As a result Student is not a valid subclass of Person
tsTryclassPerson {#name: stringprivateage : numberconstructor(name : string,age : number) {this.#name =name this.age =age }}classClass 'Student' incorrectly extends base class 'Person'. Types have separate declarations of a private property 'age'.2415Class 'Student' incorrectly extends base class 'Person'. Types have separate declarations of a private property 'age'.extends Student Person {#name: string | string[]privateage : numberconstructor(name : string,age : number | null) {super(name ,age || 0)this.#name =name this.Type 'number | null' is not assignable to type 'number'. Type 'null' is not assignable to type 'number'.2322Type 'number | null' is not assignable to type 'number'. Type 'null' is not assignable to type 'number'.age =age }}
Example 13
ts
Click here for the answer
Yes, this will compile. Because Ecma #private fields are not visible,
even at runtime, outside of the class, there’s no collision between the
two #name members.
tsTryclassPerson {#name: stringconstructor(name : string) {this.#name =name }}functionmakeName (name : string | string[]): string {if (Array .isArray (name )) returnname .join (" ")else returnname }classStudent extendsPerson {#name: string | string[]constructor(name : string | string[]) {super(makeName (name ))this.#name =name }}