Skip to content
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

Infer between closely matching types in unions and intersections #32558

Merged
merged 10 commits into from Jul 29, 2019

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Jul 25, 2019

This PR further extends the work in #32460. The complete rules for union and intersection type inference are summarized in the following.

When inferring from a set of source types in a single type or a union of types to a set of target types in a union type:

  • First, infer between types that are identical in the source and target sets and eliminate those types from the source and target sets.
  • Next, infer between types that closely match in the source and target sets and eliminate those types from the source and target sets. Types closely match when they are instantiations of the same object type or instantiations of the same type alias.
  • Next, infer from each type in the source set to each type in the target set that isn't a naked type parameter, tracking for each source type whether any inferences were made from that type.
  • Finally, if there were source types from which no inferences were made in the previous step, infer from a union of those source types to each naked type parameter in the target set. Give lower priority to these inferences if the target set contains more than one naked type parameter.

When inferring from a set of source types in a single type or an intersection of types to a set of target types in an intersection type:

  • First, if the target set contains one or more naked type variable(s), infer between types that are identical in the source and target sets and eliminate those types from the source and target sets.
  • Next, infer from each type in the source set to each type in the target set that isn't a naked type parameter.
  • Finally, if the target set contains exactly one naked type variable, infer from each type in the source set to that naked type variable. Give lower priority to these inferences.

The rationale for eliminating identical types only when the target set of an intersection contains naked type variables is that when inferring from string[] & { extra: any } to string[] & T we want to remove string[] and infer { extra: any } for T, but when inferring to string[] & Iterable<T> we want to keep the string[] on the source side and infer string for T.

Fixes #32247.
Fixes #32572.

@ahejlsberg
Copy link
Member Author

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 25, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 4c76bae. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg
Copy link
Member Author

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 26, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 00f41e5. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg
Copy link
Member Author

DT tests look good. Only change is two new errors in baconjs/baconjs-tests.ts, both of which are because we make better inferences that reveal an actual type error.

@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 28, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at a9e0a77. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 28, 2019

Heya @ahejlsberg, I've started to run the extended test suite on this PR at a9e0a77. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg
Copy link
Member Author

DT tests still look good. There are minor changes in three projects in the RWC tests. I have reviewed all of them and they're fine. I think this PR is ready to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrong type infer in 3.6.0-dev.20190725 Error with async generators
2 participants