0
0
Typescriptprogramming~5 mins

Generic arrow functions in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a generic arrow function in TypeScript?
A generic arrow function is a function written with arrow syntax that uses type parameters to work with different data types while keeping type safety.
Click to reveal answer
beginner
How do you declare a generic arrow function with one type parameter <T>?
You write the function with <T> before the parameters, like: <br><code>const func = &lt;T&gt;(arg: T): T =&gt; arg;</code>
Click to reveal answer
intermediate
Why use generic arrow functions instead of regular functions?
Generic arrow functions provide concise syntax and allow type flexibility with less code, making them easy to read and maintain.
Click to reveal answer
beginner
What does this generic arrow function do?<br><code>const identity = &lt;T&gt;(value: T): T =&gt; value;</code>
It returns the same value it receives, keeping the type of the input and output the same.
Click to reveal answer
intermediate
Can generic arrow functions have multiple type parameters? Give an example.
Yes, for example:<br><code>const pair = &lt;T, U&gt;(first: T, second: U): [T, U] =&gt; [first, second];</code>
Click to reveal answer
How do you start declaring a generic arrow function with type parameter T?
Aconst func = <T>(arg: T) => arg;
Bfunction func<T>(arg: T) { return arg; }
Cconst func = (arg: T) => arg;
Dconst func = (T) => arg;
What does the generic arrow function <T>(arg: T): T => arg; do?
AThrows an error.
BConverts the argument to string.
CReturns the argument with the same type.
DReturns a number always.
Can generic arrow functions have more than one type parameter?
AYes, like <T, U>.
BNo, only one type parameter is allowed.
COnly if you use function keyword.
DOnly in classes.
Which of these is a valid generic arrow function syntax?
Aconst func = (arg: T) <T> => arg;
Bconst func = (T)<arg>: T => arg;
Cconst func = <T> arg: T => arg;
Dconst func = <T>(arg: T): T => arg;
What is the main benefit of using generic arrow functions?
AThey run faster than normal functions.
BThey allow writing flexible, type-safe functions with concise syntax.
CThey do not require type annotations.
DThey only work with strings.
Explain how to write a generic arrow function that returns the input value unchanged.
Think about how to declare <T> and use it in parameters and return type.
You got /4 concepts.
    Describe the advantages of using generic arrow functions in TypeScript.
    Consider why generics and arrow functions are useful together.
    You got /4 concepts.