Continue inferring single generic signature's type parameters in presence of errors #19159
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #18715
Type inference initially skips context-sensitive arguments when inferring type parameters. After it has inferred all it can from the non-context-sensitive arguments, it enables the context-sensitive arguments one at a time and retries inference each time.
Previously, this process would stop as soon as inferences resulted in an erroneous signature -- one that was not assignable to the original signature. But stopping early can miss inferences from context-sensitive arguments that might not result in errors. This changes
chooseOverload
to continue inferring from context-sensitive arguments, even when a previous set of inferences resulted in an erroneous signature.chooseOverload
only gives up if inferring from all the arguments results in an error. This improves inference in tricky cases like this:Here, the only inferences come from the context-sensitive object literal -- inferring from
"d"
tokeyof T
gives nothing. But those inferences come in the second round, even though the first round is an error after inferringT={}
, because"d"
is not inkeyof {}
.Notably, however, this improvement only applies to generic signatures without overloads. The resolution process interleaves overload checking with the progressive checking of context-sensitive arguments. Because inference can fix the types of context-sensitive arguments, checking more context-sensitive arguments after encountering an error can leave those arguments fixed to types that are known to be incorrect.
For example:
If this improvement were applied to multiple overloads, then, when
createMap<string>()
failed to match the first overload, inference would continue on to inference fromkey => 101
. It would inferT=number
, but it would also fix the type ofkey
to__String
. Then it would fail to match the first overload again (createMap<string>()
is still of typeMap<string>
notMap<__String>
). When checking the second overload, though,key
would still be fixed to__String
, so even thoughcreateMap<string>()
would match,key
would not.