0
0
Typescriptprogramming~5 mins

Explicit type annotations in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an explicit type annotation in TypeScript?
An explicit type annotation is when you directly specify the type of a variable, function parameter, or return value using a colon and the type name, like <code>let age: number;</code>. It helps the compiler know exactly what type to expect.
Click to reveal answer
beginner
Why use explicit type annotations instead of relying on TypeScript's type inference?
Explicit type annotations make your code clearer and easier to understand. They also help catch mistakes early by ensuring variables have the exact type you want, especially when the inferred type might be too broad or unclear.
Click to reveal answer
beginner
How do you add an explicit type annotation to a function parameter in TypeScript?
You add a colon and the type after the parameter name. For example: <code>function greet(name: string) { ... }</code> means the parameter <code>name</code> must be a string.
Click to reveal answer
beginner
What happens if you assign a value of the wrong type to a variable with an explicit type annotation?
TypeScript will show an error during compilation because the value does not match the declared type. This helps prevent bugs by catching type mistakes early.
Click to reveal answer
beginner
Show an example of explicit type annotation for a variable and a function return type.
Example:<br><code>let count: number = 5;</code><br>Here, <code>count</code> is explicitly a number.<br><br>Function with return type:<br><code>function getName(): string { return 'Alice'; }</code><br>This means the function must return a string.
Click to reveal answer
What does this TypeScript code mean?<br>let price: number = 10;
AThe variable price can only hold numbers.
BThe variable price can hold any type.
CThe variable price is a string.
DThe variable price is undefined.
How do you specify a function parameter must be a string?
Afunction greet(name string) {}
Bfunction greet(name) {}
Cfunction greet(string name) {}
Dfunction greet(name: string) {}
What error will TypeScript show for this code?<br>let age: number = 'twenty';
AError: Type 'string' is not assignable to type 'number'.
BNo error, it's valid.
CError: Missing type annotation.
DError: Variable age is undefined.
Why might you add explicit return type annotations to functions?
ATo make the function run faster.
BTo avoid writing function code.
CTo help TypeScript check the function returns the correct type.
DTo allow any return type.
Which is a correct explicit type annotation for a boolean variable?
Alet isOpen = true: boolean;
Blet isOpen: boolean = true;
Clet isOpen: bool = true;
Dlet isOpen: Boolean = 'true';
Explain what explicit type annotations are and why they are useful in TypeScript.
Think about how telling the computer exactly what type a variable should be helps.
You got /4 concepts.
    Write a simple TypeScript function with explicit type annotations for parameters and return type.
    Use a function that takes a string and returns a string.
    You got /4 concepts.