-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Combine keyof T inferences #22525
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
Combine keyof T inferences #22525
Conversation
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.
Reading this, I'm not sure when inferredType
might be never, and when contravariant candidates would help. Can you point me to the PR that added this?
src/compiler/checker.ts
Outdated
@@ -11987,19 +11994,19 @@ namespace ts { | |||
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if | |||
// union types were requested or if all inferences were made from the return type position, infer a | |||
// union type. Otherwise, infer a common supertype. | |||
const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesUnion ? | |||
const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesCombination ? |
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.
can you extract this into a function similar to getContravariantInference
? Or naming it something besides unwidenedType
would make it more obvious why we check PriorityImpliesCombination and get the union, but then get the intersection if that type is never.
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.
Looks like it is #22323
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.
@sandersn how is it now?
Also fixes #22376 since it adds the lower priority for "literal"→keyof inferences. |
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.
It's very minor, but we have a conformance folder for type inference:
$conf/types/typeRelationships/typeInference
I added my test in compiler/ to match yours, but technically they should both be in conformance. It's up to you if you care.
src/compiler/checker.ts
Outdated
// Extract all object literal types and replace them with a single widened and normalized type. | ||
const candidates = widenObjectLiteralCandidates(inference.candidates); | ||
// We widen inferred literal types if | ||
// all inferences were made to top-level ocurrences of the type parameter, and |
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.
typo:occurrences
I added the test for #22376 to the PR. |
…cript into combine-keyof-inferences
@sandersn I've moved the tests |
* Combine keyof T inferences * Extract covariant inference derivation into function * Test:keyof inference lower priority than return inference for #22376 * Update 'expected' comment in keyofInferenceLowerPriorityThanReturn * Update comment in test too, not just baselines * Fix typo * Move tests
…-2.8 [release-2.8] Combine keyof T inferences (#22525)
Fixes #21553 kinda. The example will now typecheck (instead of error) as expected, but the inferred type for
T
won't be the default (as it used to be when we made no inference), instead it'll be the combination of all thekeyof T
inferences we made.This is pretty much the contravariant equivalent of how we now combine mapped type key inferences into a union.
Fixes #22376