What if your code could warn you about mistakes before you even run it?
Why Type annotation on variables in Typescript? - Purpose & Use Cases
Imagine you are writing a program where you store numbers, words, or true/false values in variables without telling anyone what type they are. Later, when you try to use these variables, you might get confused or make mistakes because you don't know what kind of data they hold.
Without clear type information, your program can break unexpectedly. You might add a number to a word by mistake or call a function on something that doesn't support it. Finding these errors by testing alone can be slow and frustrating.
Type annotation lets you tell the program exactly what kind of data each variable should hold. This helps catch mistakes early, makes your code easier to understand, and guides you while writing it.
let age; age = 25; age = 'twenty-five'; // Oops, no error here
let age: number; age = 25; age = 'twenty-five'; // Error: Type 'string' is not assignable to type 'number'
It makes your code safer and clearer, so you can build bigger programs with confidence.
Think of filling out a form where you must enter your age as a number. If someone types letters instead, the form warns them immediately. Type annotation works like that warning in your code.
Type annotation tells what kind of data a variable holds.
It helps catch mistakes before running the program.
It makes code easier to read and maintain.
Practice
let age: number; tell us about the variable age?Solution
Step 1: Understand type annotation syntax
The colon:afteragefollowed bynumbermeansagemust hold a number value.Step 2: Interpret the meaning of
The typenumbernumberrestricts the variable to only numeric values, no strings or other types allowed.Final Answer:
It meansagecan only hold numbers. -> Option BQuick Check:
Type annotation: numbermeans only numbers [OK]
- Thinking type annotation changes the value
- Confusing type annotation with assignment
- Assuming variable can hold any type without error
name in TypeScript?Solution
Step 1: Recall correct syntax for type annotation
Type annotation uses a colon:followed by the type name without quotes.Step 2: Check each option
let name: string; useslet name: string;which is correct syntax. Options B and C miss the colon or use wrong syntax. let name: "string"; uses quotes aroundstring, which is incorrect.Final Answer:
let name: string; -> Option CQuick Check:
Use colon and type name without quotes [OK]
- Omitting the colon between variable and type
- Putting type in quotes like a string value
- Using equals sign instead of colon for type
let count: number = 5; count = 10; console.log(count);
Solution
Step 1: Analyze variable declaration and assignment
countis declared as a number and initialized to 5, then reassigned to 10, which is valid.Step 2: Understand console.log output
The last assigned value is 10, soconsole.log(count)prints 10.Final Answer:
10 -> Option AQuick Check:
Variable holds last assigned number value [OK]
- Thinking initial value 5 is printed
- Expecting error on reassignment with same type
- Confusing output with undefined
let isActive: boolean = "true";
Solution
Step 1: Check type annotation and assigned value
The variableisActiveis typed asbooleanbut assigned a string value "true".Step 2: Understand TypeScript type checking
TypeScript does not allow assigning a string to a boolean variable, causing a type mismatch error.Final Answer:
Type mismatch: string assigned to boolean. -> Option DQuick Check:
Type annotation enforces matching value types [OK]
- Ignoring quotes around boolean values
- Assuming string "true" is same as boolean true
- Thinking semicolon or variable name causes error
data that can hold either a number or a string. Which is the correct TypeScript annotation?Solution
Step 1: Understand union types in TypeScript
The pipe symbol|is used to allow a variable to hold one of multiple types.Step 2: Evaluate each option
let data: number | string; usesnumber | stringwhich meansdatacan be either number or string. let data: number & string; uses&which means intersection (both types at once), invalid here. Options C and D have incorrect syntax.Final Answer:
let data: number | string; -> Option AQuick Check:
Use|for multiple allowed types [OK]
- Using & instead of | for multiple types
- Separating types with comma or space
- Putting types inside parentheses without operator
Ready to prove it?
Pass the test and this pattern moves unknown
