0
0
Typescriptprogramming~5 mins

Type annotation on function parameters in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of type annotation on function parameters in TypeScript?
Type annotation on function parameters tells TypeScript what type of value the function expects for each parameter. This helps catch errors early and improves code clarity.
Click to reveal answer
beginner
How do you add a type annotation to a function parameter in TypeScript?
You add a colon (:) after the parameter name, followed by the type. 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 pass a value of the wrong type to a function with typed parameters?
TypeScript will show an error during development, helping you fix the mistake before running the code. This prevents bugs caused by unexpected types.
Click to reveal answer
intermediate
Can function parameters have multiple types in TypeScript? How?
Yes, by using union types. For example: <code>function printId(id: number | string) { ... }</code> means <code>id</code> can be a number or a string.
Click to reveal answer
intermediate
What is the difference between optional and required parameters in TypeScript functions?
Required parameters must be provided when calling the function. Optional parameters are marked with a question mark (?), like <code>function greet(name?: string) { ... }</code>, and can be omitted.
Click to reveal answer
How do you specify that a function parameter must be a string in TypeScript?
Afunction foo(param = string) {}
Bfunction foo(param: string) {}
Cfunction foo(string param) {}
Dfunction foo(param) : string {}
What will TypeScript do if you pass a number to a function expecting a string parameter?
AThrow a runtime error
BIgnore the type mismatch
CAutomatically convert the number to string
DShow a compile-time error
How do you declare a function parameter that can accept either a number or a string?
Afunction foo(param: number | string) {}
Bfunction foo(param: number or string) {}
Cfunction foo(param: number & string) {}
Dfunction foo(param: any) {}
How do you mark a function parameter as optional in TypeScript?
Afunction foo(param?: string) {}
Bfunction foo(optional param: string) {}
Cfunction foo(param: string = optional) {}
Dfunction foo(param: string?) {}
Which of these is a correct function parameter type annotation?
Afunction add(a: number b: number) {}
Bfunction add(a number, b number) {}
Cfunction add(a: number, b: number) {}
Dfunction add(a, b): number {}
Explain how to add type annotations to function parameters in TypeScript and why it is useful.
Think about how you tell TypeScript what kind of data a function expects.
You got /4 concepts.
    Describe how to make a function parameter optional and how to allow multiple types for a parameter.
    Consider how to say a parameter can be missing or can be one of several types.
    You got /3 concepts.