Combine keyof T inferences#22525
Conversation
sandersn
left a comment
There was a problem hiding this comment.
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
| // 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.
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.
|
Also fixes #22376 since it adds the lower priority for "literal"→keyof inferences. |
sandersn
left a comment
There was a problem hiding this comment.
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 |
|
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
Twon't be the default (as it used to be when we made no inference), instead it'll be the combination of all thekeyof Tinferences we made.This is pretty much the contravariant equivalent of how we now combine mapped type key inferences into a union.
Fixes #22376