Look at this TypeScript code. What will it print when run?
function greet(name: string): string { return `Hello, ${name}!`; } console.log(greet("Alice"));
Think about what the function returns and the argument passed.
The function greet takes a string and returns a greeting string. Passing "Alice" returns "Hello, Alice!".
Which of these is the main reason to use type annotations in TypeScript?
Think about when errors are found in TypeScript compared to JavaScript.
Type annotations help the TypeScript compiler find mistakes before the program runs, making code safer.
Consider this TypeScript code without type annotations. What error will it cause?
function add(a, b) { return a + b; } console.log(add(5, "10"));
Think about what happens when you add a number and a string in JavaScript.
Without type annotations, add(5, "10") concatenates as string, resulting in "510".
What error will this TypeScript code produce when compiled?
function multiply(a: number, b: number) { return a * b; } console.log(multiply(4, "3"));
Check the types of arguments passed to the function.
TypeScript catches the wrong argument type at compile time because of type annotations.
Choose the best explanation of how type annotations help maintain large TypeScript projects.
Think about how clear type information helps developers work on code safely.
Type annotations serve as clear documentation and help tools catch mistakes early, making code easier to maintain.