-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Do not consider binding patterns in contextual types for return type … #39081
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Do not consider binding patterns in contextual types for return type … #39081
Conversation
…inference where all the signature type parameters have defaults
@typescript-bot user test this |
Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at a42e1ae. You can monitor the build here. |
Heya @weswigham, I've started to run the perf test suite on this PR at a42e1ae. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the parallelized community code test suite on this PR at a42e1ae. You can monitor the build here. |
Heya @weswigham, I've started to run the extended test suite on this PR at a42e1ae. You can monitor the build here. |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
User baselines are fine, as are rwc baselines. DT has one new failure, in const { id } = useParams();
export function useParams<Params extends { [K in keyof Params]?: string }>(): {[K in keyof Params]: Params[K] extends never ? string : Params[K]}; |
@weswigham Here they are:Comparison Report - master..39081
System
Hosts
Scenarios
|
And perf is fine, not that I expected a diff. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems totally reasonable to me. I’m a bit surprised we ever inferred from binding patterns in the first place.
@ahejlsberg wanna take a look at this? I know we've discussed how we handle binding pattern contextual types (and how odd they are) before. |
BTW, looks like this would fix #32003, yes? |
easy way to find out: @typescript-bot pack this |
Heya @weswigham, I've started to run the tarball bundle task on this PR at a42e1ae. You can monitor the build here. |
sigh man that merge commit got deleted the moment I deleted the branch. Anyways, from local testing: Yep, this fixes #32003. So long as the type parameter has a default, we'll prefer it in return type inference over a binding pattern. |
* upstream/master: LEGO: check in for master to temporary branch. Preserve newlines between try/catch/finally, if/else, do/while (microsoft#39280) not narrow static property without type annotation in constructor. (microsoft#39252) Switch to ES Map/Set internally (microsoft#33771) fix(38840): omit completions for a spread like argument in a function definition (microsoft#38897) fix(38785): include in NavigationBar child items from default exported functions (microsoft#38915) LEGO: check in for master to temporary branch. LEGO: check in for master to temporary branch. Avoid effect of element access expression (microsoft#39174) Update typescript-eslint to 3.4.1-alpha.1 (microsoft#39260) Handle 'keyof' for generic tuple types (microsoft#39218) Disable unsound T[K] rule in subtype relations (microsoft#39249) LEGO: check in for master to temporary branch. Upgrade typescript-eslint version (microsoft#39242) Handle recursive type references up to a certain level of expansion in inference (microsoft#38011) Do not consider binding patterns in contextual types for return type inference where all the signature type parameters have defaults (microsoft#39081) LEGO: check in for master to temporary branch. # Conflicts: # src/compiler/program.ts # src/compiler/types.ts
microsoft/TypeScript#39081, which ships in Typescript 4.0, stops using binding patterns as contextual types for return type inference. react-router's `useParams` relied on this to fill in an object with `any`s in case a binding pattern was used but no type argument was provided. This no longer works. This fix just adds a type argument to the tests. For two other, more complete fixes, see microsoft/TypeScript#39081 (comment) Briefly, the options are (1) go back to returning `any`-filled object types or (2) tries to default to `string`. The second fix is probably the right one, but it may hurt compilation/IDE performance, so I'll leave it to the package owners to decide whether to make that change.
microsoft/TypeScript#39081, which ships in Typescript 4.0, stops using binding patterns as contextual types for return type inference. react-router's `useParams` relied on this to fill in an object with `any`s in case a binding pattern was used but no type argument was provided. This no longer works. This fix just adds a type argument to the tests. For two other, more complete fixes, see microsoft/TypeScript#39081 (comment) Briefly, the options are (1) go back to returning `any`-filled object types or (2) tries to default to `string`. The second fix is probably the right one, but it may hurt compilation/IDE performance, so I'll leave it to the package owners to decide whether to make that change.
…ersn microsoft/TypeScript#39081, which ships in Typescript 4.0, stops using binding patterns as contextual types for return type inference. react-router's `useParams` relied on this to fill in an object with `any`s in case a binding pattern was used but no type argument was provided. This no longer works. This fix just adds a type argument to the tests. For two other, more complete fixes, see microsoft/TypeScript#39081 (comment) Briefly, the options are (1) go back to returning `any`-filled object types or (2) tries to default to `string`. The second fix is probably the right one, but it may hurt compilation/IDE performance, so I'll leave it to the package owners to decide whether to make that change.
…ersn microsoft/TypeScript#39081, which ships in Typescript 4.0, stops using binding patterns as contextual types for return type inference. react-router's `useParams` relied on this to fill in an object with `any`s in case a binding pattern was used but no type argument was provided. This no longer works. This fix just adds a type argument to the tests. For two other, more complete fixes, see microsoft/TypeScript#39081 (comment) Briefly, the options are (1) go back to returning `any`-filled object types or (2) tries to default to `string`. The second fix is probably the right one, but it may hurt compilation/IDE performance, so I'll leave it to the package owners to decide whether to make that change.
…inference where all the signature type parameters have defaults
Fixes #38969
So, when I was looking into #38969 I was tempted to call it working as intended, as it's a logical fallout of current rules for inference and contextual typing; but that felt really unsatisfying, as we know that binding pattern shapes are rather poor inference targets. I went through a few iterations, but here have settled on the simple addition of a new heuristic for inference: If all type parameters the signature we're inferring to have defaults, then we disable the usage of binding patterns in contextual type lookup, which, in turn, allows us to fallback to using those defaults, over those binding pattern types.