0
0
Typescriptprogramming~5 mins

Type alias for functions in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a type alias for functions in TypeScript?
A type alias for functions lets you give a name to a function type, making it easier to reuse and understand the function's input and output types.
Click to reveal answer
beginner
How do you define a type alias for a function that takes two numbers and returns a number?
You write: type Operation = (a: number, b: number) => number; This means Operation is a function type taking two numbers and returning a number.
Click to reveal answer
beginner
Why use a type alias for functions instead of writing the function type inline?
Using a type alias makes your code cleaner and easier to read. It also helps when you use the same function type in many places, so you only write it once.
Click to reveal answer
intermediate
Can a type alias for a function include optional parameters?
Yes! You can mark parameters as optional with a question mark. For example: type Greet = (name?: string) => void; means the function can be called with or without a name.
Click to reveal answer
intermediate
Show an example of a type alias for a function that returns a Promise of a string.
Example: type AsyncString = () => Promise<string>; This means the function returns a Promise that will give a string later.
Click to reveal answer
How do you define a type alias for a function that takes a string and returns a boolean?
Atype Check = string => boolean;
Btype Check = (input: string) => boolean;
Ctype Check = (string) => boolean;
Dtype Check = (input: string) boolean;
What does this type alias mean? type Fn = () => void;
AA function that takes no arguments and returns nothing.
BA function that takes no arguments and returns a number.
CA function that takes one argument and returns void.
DA variable that stores a function.
Can a type alias for a function include multiple parameters?
AYes, by listing parameters inside parentheses.
BNo, only one parameter is allowed.
CYes, but only if they are all the same type.
DNo, functions can't have parameters in type aliases.
What is the benefit of using a type alias for functions?
AIt removes the need to type parameters.
BIt makes the function run faster.
CIt makes code easier to read and reuse.
DIt changes the function's behavior.
How do you mark a parameter as optional in a function type alias?
AUse parentheses around the parameter.
BUse an exclamation mark after the parameter name.
CWrite 'optional' before the parameter.
DAdd a question mark after the parameter name.
Explain how to create and use a type alias for a function that adds two numbers.
Think about naming the function type and how to call a function with that type.
You got /4 concepts.
    Describe the advantages of using type aliases for functions in TypeScript projects.
    Consider how type aliases help when many functions share the same shape.
    You got /4 concepts.