Challenge - 5 Problems
Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code with variable type annotations?
Consider the following TypeScript code snippet. What will be logged to the console?
Typescript
let count: number = 5; let message: string = "Hello"; count = count + 10; console.log(message + " " + count);
Attempts:
2 left
💡 Hint
Look at how the variables are typed and how their values change before logging.
✗ Incorrect
The variable 'count' is typed as number and initialized to 5. It is then incremented by 10, making it 15. The variable 'message' is a string 'Hello'. Concatenating them with a space results in 'Hello 15'.
❓ Predict Output
intermediate2:00remaining
What error occurs when assigning wrong type to a typed variable?
What happens when you run this TypeScript code?
Typescript
let isActive: boolean = true; isActive = "yes";
Attempts:
2 left
💡 Hint
Check the type of the variable and the assigned value.
✗ Incorrect
TypeScript enforces types at compile time. Assigning a string to a boolean variable causes a compilation error.
🔧 Debug
advanced2:00remaining
Why does this TypeScript code cause a compilation error?
Identify the cause of the error in this code snippet.
Typescript
let data: number; data = 10; data = "twenty";
Attempts:
2 left
💡 Hint
Look at the type annotation and the assigned values.
✗ Incorrect
The variable 'data' is typed as number. Assigning a string 'twenty' violates the type annotation, causing a compilation error.
🧠 Conceptual
advanced2:00remaining
What is the purpose of type annotation on variables in TypeScript?
Choose the best explanation for why we use type annotations on variables.
Attempts:
2 left
💡 Hint
Think about how TypeScript helps catch mistakes before running the code.
✗ Incorrect
Type annotations help the compiler check that variables are used correctly, catching errors early and improving code quality.
❓ Predict Output
expert2:00remaining
What is the output of this TypeScript code with union type annotation?
Analyze the following code and select the correct console output.
Typescript
let value: number | string; value = 42; console.log(typeof value); value = "forty-two"; console.log(typeof value);
Attempts:
2 left
💡 Hint
Check the type of 'value' after each assignment.
✗ Incorrect
The variable 'value' can hold either a number or a string. First it holds 42 (number), then 'forty-two' (string). The typeof operator reflects these types.