0
0
Typescriptprogramming~20 mins

How TypeScript compiles to JavaScript - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Compilation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this compiled JavaScript code?

Given this TypeScript code snippet and its compiled JavaScript output, what will be printed when the JavaScript runs?

Typescript
TypeScript:
const greet = (name: string): string => {
  return `Hello, ${name}!`;
};
console.log(greet("Alice"));

Compiled JavaScript:
const greet = (name) => {
  return `Hello, ${name}!`;
};
console.log(greet("Alice"));
Aundefined
BHello, Alice!
CHello, undefined!
DSyntaxError
Attempts:
2 left
💡 Hint

Look at how the TypeScript types disappear in the compiled JavaScript but the function logic stays the same.

Predict Output
intermediate
2:00remaining
What error does this compiled JavaScript code produce?

Consider this TypeScript code and its compiled JavaScript. What error will the JavaScript code throw when run?

Typescript
TypeScript:
function add(a: number, b: number): number {
  return a + b;
}
console.log(add(5, "3"));

Compiled JavaScript:
function add(a, b) {
  return a + b;
}
console.log(add(5, "3"));
A53
B8
CSyntaxError
DTypeError: Cannot add number and string
Attempts:
2 left
💡 Hint

Remember JavaScript's behavior when adding a number and a string.

🔧 Debug
advanced
2:30remaining
Why does this compiled JavaScript code throw a ReferenceError?

Look at this TypeScript code and the compiled JavaScript. When running the JavaScript, it throws a ReferenceError. Why?

Typescript
TypeScript:
let count: number = 10;
if (count > 5) {
  let message = "Count is high";
  console.log(message);
}
console.log(message);

Compiled JavaScript:
let count = 10;
if (count > 5) {
  let message = "Count is high";
  console.log(message);
}
console.log(message);
A'message' is not declared anywhere
BBecause let variables cannot be logged
CBecause 'count' is not defined
DBecause 'message' is block-scoped and not accessible outside the if block
Attempts:
2 left
💡 Hint

Think about variable scope in JavaScript and TypeScript after compilation.

🧠 Conceptual
advanced
1:30remaining
What does the TypeScript compiler remove during compilation?

Which of the following does the TypeScript compiler remove when converting TypeScript to JavaScript?

AType annotations and interfaces
BAll variable declarations
CFunction bodies
DString literals
Attempts:
2 left
💡 Hint

Think about what JavaScript needs to run and what TypeScript adds only for development.

📝 Syntax
expert
3:00remaining
Which compiled JavaScript code matches this TypeScript enum?

Given this TypeScript enum, which JavaScript code is the correct compiled output?

Typescript
TypeScript:
enum Direction {
  Up,
  Down,
  Left,
  Right
}
console.log(Direction.Up);
A
const Direction = { Up: 0, Down: 1, Left: 2, Right: 3 };
console.log(Direction.Up);
B
enum Direction { Up: 0, Down: 1, Left: 2, Right: 3 }
console.log(Direction.Up);
C
var Direction;
(function (Direction) {
  Direction[Direction["Up"] = 0] = "Up";
  Direction[Direction["Down"] = 1] = "Down";
  Direction[Direction["Left"] = 2] = "Left";
  Direction[Direction["Right"] = 3] = "Right";
})(Direction || (Direction = {}));
console.log(Direction.Up);
D
let Direction = ['Up', 'Down', 'Left', 'Right'];
console.log(Direction[0]);
Attempts:
2 left
💡 Hint

TypeScript enums compile to a self-invoking function that creates a bidirectional mapping object.