Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Relate source types covered by a target discriminated union#30779

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
rbuckton merged 1 commit intomasterfromrelateDiscriminants
Apr 29, 2019

Conversation

@rbuckton
Copy link
Contributor

@rbucktonrbuckton commentedApr 5, 2019
edited
Loading

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 changeIteratorResult to be a discriminated union, the following example would no longer compile:

typeIteratorResult<T,TReturn=T>=|{done:false,value:T}|{done:true,value:TReturn};classNumberIterator{next(){return{done:false,value:1}}}letx:Iterator<number>=newNumberIterator();// 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:

typeS={done:boolean,value:number}typeT={done:false,value:number}// T0|{done:true,value:number}// T1
  1. Generate the a matrix of combinations:
    1. Determine the discriminantsd ofS with respect toT:["done"]
    2. Determine the discriminant typesdt ofd:[[true, false]]
    3. Generate the matrix of combinationsc:[[true], [false]]
  2. FilterT to the matching discriminantscm ofc:[T0, T1]
    1. Determine thatcm has at least one match for each combination inc.
  3. Relate the remaining non-discriminants (["value"]) ofS andcm.
    1. Determine thatS["value"] is related to bothT0["value"] andT1["value"], thereforeS is related toT.

Further examples can be found inassignmentCompatWithDiscriminatedUnion.ts

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

niieani, bradenhs, adidahiya, sutarmin, and gitowiec reacted with thumbs up emoji
@rbuckton
Copy link
ContributorAuthor

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

@typescript-bot
Copy link
Collaborator

typescript-bot commentedApr 5, 2019
edited
Loading

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

@typescript-bot
Copy link
Collaborator

typescript-bot commentedApr 5, 2019
edited
Loading

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

@rbuckton
Copy link
ContributorAuthor

@typescript-bot perf test

@typescript-bot
Copy link
Collaborator

typescript-bot commentedApr 6, 2019
edited
Loading

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

@microsoftmicrosoft deleted a comment fromtypescript-botApr 6, 2019
@microsoftmicrosoft deleted a comment fromtypescript-botApr 6, 2019
@rbuckton
Copy link
ContributorAuthor

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

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh
Copy link
Member

Also#20889

@rbuckton
Copy link
ContributorAuthor

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

@rbuckton
Copy link
ContributorAuthor

@ahejlsberg: Can you take a look?

Copy link
Member

@ahejlsbergahejlsberg left a comment

Choose a reason for hiding this comment

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

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

@rbucktonrbuckton merged commit2d8527f intomasterApr 29, 2019
@rbucktonrbuckton deleted the relateDiscriminants branchApril 29, 2019 23:58
@jcalz
Copy link
Contributor

jcalz commentedMay 30, 2019
edited
Loading

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.

@microsoftmicrosoft locked asresolvedand limited conversation to collaboratorsOct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@weswighamweswighamweswigham approved these changes

@ahejlsbergahejlsbergahejlsberg approved these changes

@DanielRosenwasserDanielRosenwasserAwaiting requested review from DanielRosenwasser

@RyanCavanaughRyanCavanaughAwaiting requested review from RyanCavanaugh

@sandersnsandersnAwaiting requested review from sandersn

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

6 participants

@rbuckton@typescript-bot@RyanCavanaugh@jcalz@weswigham@ahejlsberg

[8]ページ先頭

©2009-2025 Movatter.jp