0
0
Typescriptprogramming~5 mins

Multiple generic parameters in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are multiple generic parameters in TypeScript?
They are placeholders for types that allow a function, class, or interface to work with more than one type, making the code flexible and reusable.
Click to reveal answer
beginner
How do you declare a function with two generic parameters in TypeScript?
Use angle brackets with two type variables separated by a comma, like , before the function parameters.
Click to reveal answer
beginner
Example: What does this function do? function pair(first: T, second: U): [T, U] { return [first, second]; }
It takes two values of any types T and U, and returns a tuple (pair) containing both values in order.
Click to reveal answer
intermediate
Why use multiple generic parameters instead of just one?
Because you might want to work with two or more different types at once, keeping their types separate and flexible.
Click to reveal answer
intermediate
Can generic parameters have constraints when using multiple generics?
Yes, each generic parameter can have its own constraint to limit what types it can accept, improving type safety.
Click to reveal answer
How do you declare a function with two generic types T and U in TypeScript?
Afunction example<T & U>() {}
Bfunction example<T U>() {}
Cfunction example<T, U>() {}
Dfunction example<T; U>() {}
What does this function return? function combine(a: T, b: U): [T, U] { return [a, b]; }
AAn array with elements of types T and U
BA single value of type T
CA single value of type U
DA string combining a and b
Can each generic parameter have its own constraint in multiple generics?
AYes, each can have different constraints
BNo, constraints apply to all generics together
COnly one generic can have a constraint
DConstraints are not allowed with multiple generics
Why use multiple generic parameters instead of one?
ATo make code slower
BTo force all types to be the same
CTo avoid using types
DTo handle multiple different types separately
Which of these is a valid generic function with two parameters?
Afunction f<T; U>(x: T, y: U) {}
Bfunction f<T, U>(x: T, y: U) {}
Cfunction f<T U>(x: T, y: U) {}
Dfunction f<T & U>(x: T, y: U) {}
Explain how to declare and use a function with multiple generic parameters in TypeScript.
Think about how you can pass two different types and keep them separate.
You got /4 concepts.
    Describe why multiple generic parameters are useful and how constraints can be applied to them.
    Consider real-life situations where you want to keep two things different but related.
    You got /3 concepts.