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

Relate source types covered by a target discriminated union #30779

Merged
merged 1 commit into from Apr 29, 2019

Conversation

Projects
None yet
6 participants
@rbuckton
Copy link
Member

commented Apr 5, 2019

This PR effects how we relate a source "Object" type to a target union type when:

  1. The source is not relatable to a single type in the target union.
  2. The target union has discriminant properties.
  3. The source type would be sufficiently covered by all constituents of the union.

For example, today if we were to change IteratorResult to be a discriminated union, the following example would no longer compile:

type IteratorResult<T, TReturn = T> =
  | { done: false, value: T }
  | { done: true, value: TReturn };

class NumberIterator {
  next() {
    return { done: false, value: 1 }
  }
}

let x: Iterator<number> = new NumberIterator();
// error TS2322: Type 'NumberIterator' is not assignable to type 'IteratorResult<number>'.
//   Type '{ done: boolean, value: number }' is not assignable to type '{ done: true, value: number }'.
//     ...

With this change, we will perform the following steps after all other relationship checks fail:

If 'source' is an Object type and 'target' is a Union type with discriminant properties:

  1. Generate a matrix of combinations of discriminant properties and types 'source' can satisfy.
    1. If the number of combinations is above a fixed limit (currently 25), we consider the comparison too complex and determine that 'source' is not related to the 'target'.
  2. Filter 'target' to the subset of constituents whose discriminants exist in the matrix.
    1. If any combination of types in the matrix does not have a match in 'target', we determine 'source' is not related to 'target'.
  3. For each matching constituent of 'target', we relate the remaining non-discriminant properties of 'source' to 'target'.
    1. If any remaining properties are not related, we determine 'source' is not related to 'target'
    2. Otherwise, 'source' is sufficiently covered by all constituents of 'target' and they are related.

This means that now, for the example above, we perform the following steps:

type S = { done: boolean, value: number }
type T = { done: false, value: number } // T0
       | { done: true, value: number }  // T1
  1. Generate the a matrix of combinations:
    1. Determine the discriminants d of S with respect to T: ["done"]
    2. Determine the discriminant types dt of d: [[true, false]]
    3. Generate the matrix of combinations c: [[true], [false]]
  2. Filter T to the matching discriminants cm of c: [T0, T1]
    1. Determine that cm has at least one match for each combination in c.
  3. Relate the remaining non-discriminants (["value"]) of S and cm.
    1. Determine that S["value"] is related to both T0["value"] and T1["value"], therefore S is related to T.

Further examples can be found in assignmentCompatWithDiscriminatedUnion.ts

Related: #2983
Fixes: #14865
Fixes: #30871
Fixes: #30170
Fixes: #12052
Fixes: #18421
Fixes: #15907
Fixes: #20889

@rbuckton

This comment has been minimized.

Copy link
Member Author

commented Apr 5, 2019

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

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 5, 2019

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

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 5, 2019

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

@rbuckton

This comment has been minimized.

Copy link
Member Author

commented Apr 6, 2019

@typescript-bot perf test

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 6, 2019

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

@microsoft microsoft deleted a comment from typescript-bot Apr 6, 2019

@microsoft microsoft deleted a comment from typescript-bot Apr 6, 2019

@rbuckton

This comment has been minimized.

Copy link
Member Author

commented Apr 6, 2019

Pardon the dust, tinkering with the on-demand benchmark script to not generate a massive wall of text when posting the results.

@RyanCavanaugh

This comment has been minimized.

Copy link
Member

commented Apr 19, 2019

Possibly fixes? #30170 #12052 #18421 #12052 #15907

@RyanCavanaugh

This comment has been minimized.

Copy link
Member

commented Apr 19, 2019

Also #20889

@rbuckton rbuckton force-pushed the relateDiscriminants branch from 8321adf to 332ea0a Apr 19, 2019

@rbuckton

This comment has been minimized.

Copy link
Member Author

commented Apr 19, 2019

@RyanCavanaugh: I've added tests and verified that this does indeed fix those issues.

@rbuckton

This comment has been minimized.

Copy link
Member Author

commented Apr 19, 2019

@ahejlsberg: Can you take a look?

@ahejlsberg
Copy link
Member

left a comment

I'd inline the constant instead of shouting all caps.

@rbuckton rbuckton force-pushed the relateDiscriminants branch from 332ea0a to db7f701 Apr 29, 2019

@rbuckton rbuckton force-pushed the relateDiscriminants branch from db7f701 to 26fd6da Apr 29, 2019

@rbuckton rbuckton merged commit 2d8527f into master Apr 29, 2019

5 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
node10 Build #29413 succeeded
Details
node11 Build #29411 succeeded
Details
node8 Build #29412 succeeded
Details

@rbuckton rbuckton deleted the relateDiscriminants branch Apr 29, 2019

@jcalz

This comment has been minimized.

Copy link
Contributor

commented May 30, 2019

Hmm, why doesn't #12052 seem to link back to this issue? Oh, does locking an issue actually prevent it from getting new "@somebody mentioned this" links? That's too bad... it becomes much harder to follow up on these longstanding issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.