All quizzesMedium
TypeScript Intermediate — Series 2
Preview — 3 of 10 questions
What JavaScript does TypeScript emit for this const enum reference?
javascript
const enum Status {
Active,
Inactive,
}
let s = Status.Active;Aconst enum members can't be accessed at all outside the file where they're declared
BStatus.Active is inlined as 0; no enum object is emitted.
Cconst enum requires every member to have an explicit value
Dconst enum behaves exactly like a regular enum — const is purely cosmetic and has no compilation effect
Do both branches type-check after the instanceof test?
javascript
class Dog { bark() { return "Woof"; } }
class Cat { meow() { return "Meow"; } }
function speak(animal: Dog | Cat) {
if (animal instanceof Dog) {
return animal.bark();
}
return animal.meow();
}ACompile-time error on animal.bark() — animal is still typed Dog | Cat inside the if block
BCompile-time error on animal.meow() — Dog was already checked first
CBoth branches compile: instanceof narrows animal to Dog and then Cat.
DCompiles fine, but only after adding an explicit as Dog / as Cat assertion
How does checking kind affect the type of shape?
javascript
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number };
function area(shape: Shape): number {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}
return shape.side ** 2;
}AThe kind check narrows shape to the member with that literal discriminant.
Bradius and side are treated as optional properties present on every member of the union
CTypeScript widens shape to any inside conditional branches, so any property access is allowed
DThis is a compile-time error — shape.radius isn't accessible without an explicit type assertion
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.