-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic Tips Part 1: Use Classes and Currying to create new inference sites #22
Comments
Hi First of really good post! I just wonder the first declaration declare function getUrl<API, Path extends keyof API>(path: Path, params: ExtractRouteParams<Path>); is equivalent with the following (assuming we do not need the declare function getUrl<API>(path: keyof API, params: ExtractRouteParams<typeof path>); It would spare the whole hussle of all-or-nothing type interference (at least in this example). Thank you for the answer. |
Similar question: this would be also the very same? function getUrl<API, Path=keyof API>(path: Path, params: ExtractRouteParams<Path>) |
@arvabalazs interesting question. Neither of those declarations is quite the same. For this declaration: declare function getUrl<API>(path: keyof API, params: ExtractRouteParams<typeof path>): string; is that The second declaration has the same problem: declare function getUrl<API, Path=keyof API>(path: Path, params: ExtractRouteParams<Path>): string; In this case, if you omit the second type parameter ( Until there's movement on microsoft/TypeScript#10571, you really do need to use the techniques described in this article to get partial inference. |
Thank you @dankv! Your explanation helps me a lot to build up my mental model about this language. |
Typescript 5.4 adds |
Generic Tips Part 1: Use Classes and Currying to create new inference sites
Effective TypeScript: Generic Tips Part 1: Use Classes and Currying to create new inference sites
https://effectivetypescript.com/2020/12/04/gentips-1-curry/
The text was updated successfully, but these errors were encountered: