Consider the following TypeScript code snippet. What will be printed to the console when you run it using ts-node?
const greet = (name: string): string => { return `Hello, ${name}!`; }; console.log(greet("Alice"));
Think about how arrow functions and template strings work in TypeScript and JavaScript.
The function greet takes a string and returns a greeting using a template string. When run with ts-node, it compiles and executes correctly, printing Hello, Alice!.
You want to run a TypeScript file named app.ts without compiling it first. Which command should you use?
Remember, ts-node runs TypeScript files directly without manual compilation.
The ts-node command runs TypeScript files directly. Using node app.ts will fail because Node.js does not understand TypeScript syntax. Option D compiles then runs but is not direct. Option D is not a valid npm command.
Look at this TypeScript code. When run with ts-node, what error will it produce?
function add(a: number, b: number): number { return a + b; } console.log(add(5, "3"));
Think about TypeScript's type checking during execution with ts-node.
TypeScript checks types at compile time, but ts-node performs compilation and execution together. Passing a string where a number is expected causes a type error: Argument of type 'string' is not assignable to parameter of type 'number'.
Identify which code snippet will cause a syntax error when executed with ts-node.
Look carefully at string literals and missing characters.
Option A has a missing closing quote for the string, causing a syntax error. Other options are syntactically correct.
Consider this TypeScript code run with ts-node. How many elements does the result array contain?
const numbers = [1, 2, 3, 4, 5]; const result = numbers.map(n => { if (n % 2 === 0) { return n * 2; } }); console.log(result);
Remember how map works and what happens when a function returns undefined.
The map method returns an array of the same length as the original. For odd numbers, the function returns undefined. So result has 5 elements, some are numbers, some are undefined.