Generics in Function Overloads
This exercise is a little bit tricky.
Here's a function called returnWhatIPassInExceptFor1
:
function returnWhatIPassInExceptFor1(t: unknown): unknown { if (t === 1) { return 2; } return t;}
When I pass in anything other than 1
, the function should return what I p
Transcript
0:00 This exercise is a little bit tricky. This function says returnWhatIPassInExceptFor1. What I want this function to do is basically return what I pass in, so this should be typed as a, this should be typed as b, this should be typed as c, except for when you pass in 1 here.
0:21 You basically want to have...You're probably going to need a generic here to pass in through all the stuff when you just pass in whatever you want to, but when you return in 1, the result should be of type 2.
0:34 You'll need some function overloads here. You'll also need a generic. That, I think, is all the clues I'm going to give you. Good luck.