Skip to content

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

Merged
merged 9 commits into from
Mar 19, 2018

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Mar 13, 2018

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 the keyof 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

Copy link
Member

@sandersn sandersn left a 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?

@@ -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 ?
Copy link
Member

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.

Copy link
Member

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

Copy link
Member Author

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?

@sandersn
Copy link
Member

sandersn commented Mar 14, 2018

Also fixes #22376 since it adds the lower priority for "literal"→keyof inferences.

Copy link
Member

@sandersn sandersn left a 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.

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:occurrences

@sandersn
Copy link
Member

I added the test for #22376 to the PR.

@weswigham
Copy link
Member Author

@sandersn I've moved the tests

@weswigham weswigham merged commit eaabf92 into microsoft:master Mar 19, 2018
@weswigham weswigham deleted the combine-keyof-inferences branch March 19, 2018 23:56
DanielRosenwasser pushed a commit that referenced this pull request Mar 20, 2018
* 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
DanielRosenwasser added a commit that referenced this pull request Mar 20, 2018
…-2.8

[release-2.8] Combine keyof T inferences (#22525)
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants