Given this TypeScript code snippet and its compiled JavaScript output, what will be printed when the JavaScript runs?
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"));
Look at how the TypeScript types disappear in the compiled JavaScript but the function logic stays the same.
The TypeScript types are removed during compilation. The JavaScript function still returns the greeting string with the name passed in, so it prints "Hello, Alice!".
Consider this TypeScript code and its compiled JavaScript. What error will the JavaScript code throw when run?
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"));
Remember JavaScript's behavior when adding a number and a string.
JavaScript converts the number 5 to a string and concatenates it with "3", resulting in the string "53".
Look at this TypeScript code and the compiled JavaScript. When running the JavaScript, it throws a ReferenceError. Why?
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);
Think about variable scope in JavaScript and TypeScript after compilation.
The variable 'message' is declared with let inside the if block, so it only exists inside that block. The last console.log tries to access it outside, causing a ReferenceError.
Which of the following does the TypeScript compiler remove when converting TypeScript to JavaScript?
Think about what JavaScript needs to run and what TypeScript adds only for development.
TypeScript removes type annotations and interfaces because JavaScript does not support types natively. It keeps variable declarations and function bodies intact.
Given this TypeScript enum, which JavaScript code is the correct compiled output?
TypeScript:
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction.Up);TypeScript enums compile to a self-invoking function that creates a bidirectional mapping object.
TypeScript compiles enums into an object with numeric keys and string values for reverse lookup, using an IIFE to initialize it. Option C shows this pattern.