Typing Functions with Generics
In this exercise, we have a function called returnWhatIPassIn
which takes a parameter t
of type unknown
:
const returnWhatIPassIn = (t: unknown) => { return t;};
Currently, this is typed as unknown
, so if you hover over this, it's going to return unknown
as well.
Bec
Transcript
0:00 In this exercise, we have a function called returnWhatIPassIn. What we need to do is anything that we pass this function, we're just going to return it. Currently, this is typed as unknown. If you hover over this, then it's going to return unknown as well.
0:15 This isn't correct. We want it to return 1 if we pass in 1 and return matt if we pass in matt. These two types here are currently being inferred as unknown. Your job is to try to find a syntax that we can type this function with that will let us return whatever we want or rather, return what gets passed in.
0:37 You'll need to look in the generics section in terms of typing a function with a generic type. Good luck.