The Partial Inference Problem
There are some really annoying aspects of how TypeScript infers type arguments when they are passed into functions.
Here is a
makeSelectors function that takes in a
TSource type argument and then a
TSelectors type argument:
export const makeSelectors = <TSource,TSelectors
0:00 In this exercise, we're covering a really annoying aspect of how TypeScript infers type arguments when you pass them to functions.
0:07 We have a makeSelectors function, and this takes in a TSource type argument, and then a TSelectors type argument. Now, the best way to look at this is through usage. We have a makeSelectors function here where we're passing it a type argument, except that it looks like the second type argument.
0:27 Instead of being inferred as the selectors, which is a record of string, and then a function which returns source, TSource, any, then we're not getting that inferred there. If we remove the source, then we're going to get that inferred. So getFullName, source any string, getFirstNameLength, source any, any.
0:47 This is tough because it looks like as soon as we pass in one type argument, the second type argument doesn't get inferred. This is correct. This is how TypeScript works, and it's so annoying. It's annoying because you can't pass in type arguments and infer the actual arguments in the same function call.
1:10 We're going to have to make a compromise here. We're going to slightly compromise our API in order to get the inference that we want. We're going to add in a function call here. We're going to say makeSelectors, passing in the type argument of source. We're going to call that function and then call it again with the selectors.
1:30 You can see that the tests here are currently just not working because we're expecting selectors to have an attribute of getFullName, which is going to be source, Source String, getFirstAndLastName, source string, getFirstNameLength, source number. This is a tough problem. You have all the pieces that you need to understand the solution. Good luck.