TypeScript Fundamentals — Series 3

Preview — 3 of 10 questions

What does the emitted JavaScript contain, and what is logged?

javascript
interface User {
  id: number;
  name: string;
}

const user: User = { id: 1, name: "Ada" };
console.log(typeof user);
AThe emitted JS keeps the interface declaration, and "User" is logged
BThe emitted JS keeps the type annotation : User on the constant, and "User" is logged
CThe interface and the : User annotation are both erased, and "object" is logged
DCompile-time error — typeof cannot be used on a value with an interface type

javascript
let score: number = 10;
score = "10";
ACompiles fine — TypeScript converts "10" to the number 10
BCompile-time error — score is annotated number, so a string isn't assignable to it
CCompiles fine, but score becomes NaN at runtime
DCompiles fine — the annotation only applies to the initial value

javascript
const ids: Array<number> = [1, 2, 3];
ids.push(4);
ids.push("5");
ABoth push calls compile — arrays accept any value at runtime
BCompile-time error on ids.push(4) — Array<number> is read-only
CBoth push calls compile, but "5" is coerced to the number 5
Dids.push(4) compiles; ids.push("5") is a compile-time error — the element type is number

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.