codejump

TypeScript Fundamentals — Series 2

Preview — 3 of 10 questions

What does this code print?

javascript
enum Direction {
  Up = "UP",
  Down = "DOWN",
}
console.log(Direction.Up);
A0
B"UP"
C"Up"
Dundefined

Does the assignment to point type-check?

javascript
let point: [number, number] = [10, 20];
point = [10, 20, 30];
ACompiles fine — tuples can have extra elements as long as the first ones match
BCompiles fine, but point[2] is undefined at runtime
CType error: a three-element array cannot be assigned to a two-element tuple.
DRuntime error: "too many array elements"

Does this call to greet type-check?

javascript
function greet(name: string, times: number): string {
  return name.repeat(times);
}
greet("Hi", "3");
AType error: the second argument is a string, but times requires a number.
BCompiles fine — TypeScript automatically coerces "3" to the number 3
CCompiles fine because function parameter types are only checked at runtime
DCompile-time error, but only because greet was called with the wrong number of arguments

Sign up free to play

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