Skip to content

Reuse "getBestMatchingType" logic during elaboration to allow for more specific elaborations #35278

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

Conversation

weswigham
Copy link
Member

Fixes #35248

@DanielRosenwasser
Copy link
Member

The original example just used number because it was easy. How does this work for unions with object types?

interface Stuff {
  a?: () => Promise<number[]>;
  b: () => Promise<string>;
  c: () => Promise<string>;
  d: () => Promise<string>;
  e: () => Promise<string>;
  f: () => Promise<string>;
  g: () => Promise<string>;
  h: () => Promise<string>;
  i: () => Promise<string>;
  j: () => Promise<string>;
  k: () => Promise<number>;
}

function foo(): Stuff | Date {
  return {
    a() { return [123] },
    b: () => "hello",
    c: () => "hello",
    d: () => "hello",
    e: () => "hello",
    f: () => "hello",
    g: () => "hello",
    h: () => "hello",
    i: () => "hello",
    j: () => "hello",
    k: () => 123
  }
}

@weswigham
Copy link
Member Author

How does this work for unions with object types?

It doesn't~

@weswigham weswigham changed the title Filter target union during elaboration to allow for more specific elaborations Reuse "getBestMatchingType" logic during elaboration to allow for more specific elaborations Nov 21, 2019
@weswigham
Copy link
Member Author

@DanielRosenwasser and now it does

@DanielRosenwasser
Copy link
Member

@typescript-bot test this
@typescript-bot user test this
@typescript-bot perf test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 21, 2019

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 21, 2019

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 21, 2019

Heya @DanielRosenwasser, I've started to run the parallelized community code test suite on this PR at 476318e. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@weswigham
Copy link
Member Author

@DanielRosenwasser I merged the user update into this PR and the RWC update looks good.

@typescript-bot
Copy link
Collaborator

@DanielRosenwasser
The results of the perf run you requested are in!

Here they are:

Comparison Report - master..35278

Metric master 35278 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 354,702k (± 0.02%) 356,465k (± 0.02%) +1,763k (+ 0.50%) 356,366k 356,640k
Parse Time 1.62s (± 0.79%) 1.63s (± 0.56%) +0.01s (+ 0.49%) 1.61s 1.65s
Bind Time 0.86s (± 0.77%) 0.87s (± 0.51%) +0.01s (+ 0.58%) 0.86s 0.88s
Check Time 4.53s (± 0.67%) 4.51s (± 0.47%) -0.02s (- 0.38%) 4.45s 4.55s
Emit Time 5.29s (± 0.84%) 5.28s (± 0.76%) -0.00s (- 0.04%) 5.20s 5.35s
Total Time 12.30s (± 0.42%) 12.29s (± 0.44%) -0.00s (- 0.01%) 12.18s 12.41s
Monaco - node (v10.16.3, x64)
Memory used 365,967k (± 0.01%) 365,952k (± 0.02%) -15k (- 0.00%) 365,849k 366,083k
Parse Time 1.26s (± 0.53%) 1.27s (± 0.70%) +0.01s (+ 0.48%) 1.25s 1.29s
Bind Time 0.76s (± 0.63%) 0.75s (± 0.53%) -0.01s (- 0.79%) 0.74s 0.76s
Check Time 4.65s (± 0.79%) 4.64s (± 0.48%) -0.01s (- 0.24%) 4.57s 4.68s
Emit Time 2.97s (± 0.43%) 2.95s (± 0.94%) -0.02s (- 0.54%) 2.90s 3.02s
Total Time 9.63s (± 0.56%) 9.60s (± 0.45%) -0.03s (- 0.30%) 9.49s 9.70s
TFS - node (v10.16.3, x64)
Memory used 321,678k (± 0.01%) 321,685k (± 0.02%) +8k (+ 0.00%) 321,512k 321,831k
Parse Time 0.96s (± 0.52%) 0.96s (± 0.90%) +0.00s (+ 0.21%) 0.95s 0.98s
Bind Time 0.73s (± 1.14%) 0.73s (± 1.83%) +0.00s (+ 0.14%) 0.69s 0.75s
Check Time 4.12s (± 0.49%) 4.12s (± 0.48%) +0.00s (+ 0.07%) 4.08s 4.16s
Emit Time 3.07s (± 0.67%) 3.06s (± 0.80%) -0.01s (- 0.39%) 2.98s 3.09s
Total Time 8.87s (± 0.36%) 8.86s (± 0.57%) -0.01s (- 0.11%) 8.75s 8.97s
Angular - node (v12.1.0, x64)
Memory used 330,323k (± 0.02%) 331,705k (± 0.10%) +1,381k (+ 0.42%) 330,717k 332,070k
Parse Time 1.57s (± 0.48%) 1.58s (± 0.55%) +0.02s (+ 1.02%) 1.56s 1.60s
Bind Time 0.85s (± 0.66%) 0.86s (± 0.87%) +0.01s (+ 1.06%) 0.84s 0.87s
Check Time 4.45s (± 0.52%) 4.45s (± 0.63%) +0.00s (+ 0.04%) 4.39s 4.50s
Emit Time 5.51s (± 0.99%) 5.52s (± 1.29%) +0.01s (+ 0.09%) 5.40s 5.69s
Total Time 12.38s (± 0.60%) 12.40s (± 0.66%) +0.03s (+ 0.20%) 12.25s 12.63s
Monaco - node (v12.1.0, x64)
Memory used 345,712k (± 0.02%) 345,621k (± 0.01%) -92k (- 0.03%) 345,543k 345,711k
Parse Time 1.23s (± 0.68%) 1.22s (± 0.85%) -0.01s (- 0.57%) 1.20s 1.24s
Bind Time 0.73s (± 1.06%) 0.72s (± 0.90%) -0.01s (- 1.23%) 0.71s 0.74s
Check Time 4.48s (± 0.40%) 4.47s (± 0.62%) -0.01s (- 0.16%) 4.42s 4.55s
Emit Time 3.02s (± 0.80%) 3.01s (± 0.74%) -0.01s (- 0.40%) 2.96s 3.08s
Total Time 9.45s (± 0.37%) 9.42s (± 0.43%) -0.03s (- 0.34%) 9.35s 9.53s
TFS - node (v12.1.0, x64)
Memory used 304,053k (± 0.02%) 304,057k (± 0.02%) +4k (+ 0.00%) 303,945k 304,209k
Parse Time 0.95s (± 0.50%) 0.95s (± 0.72%) -0.00s (- 0.11%) 0.93s 0.96s
Bind Time 0.69s (± 0.53%) 0.68s (± 0.65%) -0.00s (- 0.58%) 0.67s 0.69s
Check Time 4.06s (± 0.62%) 4.05s (± 0.37%) -0.01s (- 0.20%) 4.02s 4.08s
Emit Time 3.10s (± 1.04%) 3.10s (± 0.58%) +0.01s (+ 0.23%) 3.07s 3.15s
Total Time 8.79s (± 0.54%) 8.78s (± 0.23%) -0.00s (- 0.05%) 8.74s 8.84s
Angular - node (v8.9.0, x64)
Memory used 349,623k (± 0.02%) 351,116k (± 0.03%) +1,493k (+ 0.43%) 350,931k 351,277k
Parse Time 2.11s (± 0.35%) 2.12s (± 0.40%) +0.01s (+ 0.52%) 2.10s 2.14s
Bind Time 0.91s (± 0.93%) 0.93s (± 0.54%) +0.02s (+ 1.76%) 0.92s 0.94s
Check Time 5.26s (± 0.40%) 5.25s (± 0.94%) -0.01s (- 0.19%) 5.17s 5.38s
Emit Time 6.29s (± 0.76%) 6.28s (± 1.51%) -0.01s (- 0.17%) 6.10s 6.50s
Total Time 14.57s (± 0.50%) 14.58s (± 0.99%) +0.01s (+ 0.04%) 14.32s 14.96s
Monaco - node (v8.9.0, x64)
Memory used 363,746k (± 0.01%) 363,750k (± 0.02%) +3k (+ 0.00%) 363,621k 363,883k
Parse Time 1.56s (± 0.33%) 1.56s (± 0.50%) +0.00s (+ 0.13%) 1.54s 1.58s
Bind Time 0.93s (± 1.09%) 0.92s (± 0.89%) -0.00s (- 0.32%) 0.91s 0.94s
Check Time 5.53s (± 0.60%) 5.54s (± 0.50%) +0.01s (+ 0.11%) 5.48s 5.60s
Emit Time 3.05s (± 1.27%) 3.05s (± 0.58%) -0.00s (- 0.07%) 3.02s 3.09s
Total Time 11.07s (± 0.48%) 11.07s (± 0.34%) +0.00s (+ 0.03%) 10.99s 11.17s
TFS - node (v8.9.0, x64)
Memory used 320,585k (± 0.02%) 320,568k (± 0.01%) -17k (- 0.01%) 320,462k 320,659k
Parse Time 1.27s (± 0.41%) 1.27s (± 0.49%) -0.00s (- 0.16%) 1.26s 1.28s
Bind Time 0.74s (± 0.78%) 0.74s (± 0.90%) -0.00s (- 0.40%) 0.73s 0.76s
Check Time 4.75s (± 0.58%) 4.72s (± 0.57%) -0.03s (- 0.55%) 4.64s 4.77s
Emit Time 3.23s (± 0.85%) 3.20s (± 0.83%) -0.03s (- 0.90%) 3.15s 3.27s
Total Time 9.99s (± 0.52%) 9.93s (± 0.49%) -0.06s (- 0.62%) 9.80s 10.02s
Angular - node (v8.9.0, x86)
Memory used 198,501k (± 0.02%) 199,326k (± 0.04%) +825k (+ 0.42%) 199,186k 199,480k
Parse Time 2.02s (± 0.65%) 2.03s (± 0.43%) +0.01s (+ 0.50%) 2.01s 2.05s
Bind Time 1.03s (± 0.90%) 1.01s (± 0.57%) -0.02s (- 1.84%) 1.00s 1.03s
Check Time 4.82s (± 0.67%) 4.78s (± 0.65%) -0.04s (- 0.85%) 4.70s 4.84s
Emit Time 6.12s (± 1.78%) 6.06s (± 0.94%) -0.06s (- 0.93%) 5.97s 6.24s
Total Time 13.99s (± 0.85%) 13.88s (± 0.50%) -0.11s (- 0.77%) 13.71s 14.08s
Monaco - node (v8.9.0, x86)
Memory used 203,747k (± 0.02%) 203,770k (± 0.02%) +23k (+ 0.01%) 203,671k 203,829k
Parse Time 1.60s (± 0.78%) 1.60s (± 0.58%) -0.00s (- 0.19%) 1.59s 1.63s
Bind Time 0.76s (± 1.74%) 0.75s (± 1.08%) -0.01s (- 1.06%) 0.74s 0.77s
Check Time 5.44s (± 0.61%) 5.38s (± 1.08%) -0.05s (- 0.96%) 5.16s 5.44s
Emit Time 2.88s (± 0.54%) 2.91s (± 2.19%) +0.03s (+ 1.01%) 2.83s 3.15s
Total Time 10.68s (± 0.39%) 10.65s (± 0.22%) -0.03s (- 0.30%) 10.60s 10.71s
TFS - node (v8.9.0, x86)
Memory used 180,642k (± 0.03%) 180,628k (± 0.02%) -14k (- 0.01%) 180,545k 180,680k
Parse Time 1.31s (± 0.75%) 1.30s (± 0.67%) -0.01s (- 0.46%) 1.29s 1.33s
Bind Time 0.70s (± 0.83%) 0.70s (± 0.88%) +0.00s (+ 0.29%) 0.69s 0.71s
Check Time 4.48s (± 0.49%) 4.47s (± 0.64%) -0.01s (- 0.20%) 4.42s 4.53s
Emit Time 2.98s (± 0.88%) 2.97s (± 0.58%) -0.01s (- 0.27%) 2.92s 2.99s
Total Time 9.46s (± 0.32%) 9.44s (± 0.41%) -0.02s (- 0.19%) 9.35s 9.50s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-166-generic
Architecturex64
Available Memory16 GB
Available Memory7 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v8.9.0, x64)
  • node (v8.9.0, x86)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v8.9.0, x64)
  • Angular - node (v8.9.0, x86)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v8.9.0, x64)
  • Monaco - node (v8.9.0, x86)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v8.9.0, x64)
  • TFS - node (v8.9.0, x86)
Benchmark Name Iterations
Current 35278 10
Baseline master 10

Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

I didn't read the code, but the new error messages look great.

@@ -0,0 +1,77 @@
tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts(17,7): error TS2322: Type '() => number[]' is not assignable to type '() => Promise<number[]>'.
Type 'number[]' is missing the following properties from type 'Promise<number[]>': then, catch
Copy link
Member

Choose a reason for hiding this comment

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

As a separate change, it might be nice to special case the message Foo is not assignable to Promise<Foo> to say something about adding await, rather than listing missing properties.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's as much about missing await since it's the target - but #35300 covers the basic idea here.

@@ -0,0 +1,77 @@
tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts(17,7): error TS2322: Type '() => number[]' is not assignable to type '() => Promise<number[]>'.
Type 'number[]' is missing the following properties from type 'Promise<number[]>': then, catch
tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts(18,16): error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
Copy link
Member

Choose a reason for hiding this comment

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

But these ones didn't list missing properties?

Copy link
Member Author

Choose a reason for hiding this comment

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

Primitives skip the property listing check. An array is treated like an arbitrary object type.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Nov 22, 2019

I guess there's a risk here - if two types have the same overlappiness, the compiler skips ahead to an arbitrary level of elaboration. Imagine

type A = { a: string, b: number };
type B = { b: number, c: string };

let x: A | B = {
  b: 123,
};

That can be even more confusing for users in some cases. Maybe the filtering logic needs an option where you can set it to say of "return undefined if there's ambiguity between two constituents."

Also would like to get @RyanCavanaugh's take on these changes.

@weswigham
Copy link
Member Author

@DanielRosenwasser the error message in that case (and the excess property case) is unchanged with this, as the error for those isn't issued on the property, but rather on the parent of the property.

@weswigham
Copy link
Member Author

@DanielRosenwasser @RyanCavanaugh you wanna do a thing with this at some point?

@threehams
Copy link

I'm not either of those people, but this could fix the most common source of "I don't know where to begin" errors on my team.

@weswigham
Copy link
Member Author

@DanielRosenwasser does that 👍 mean you want me to :shipit: ?

@RyanCavanaugh
Copy link
Member

🚤

@weswigham weswigham merged commit 291ab63 into microsoft:master Jan 2, 2020
@weswigham weswigham deleted the filter-unions-when-elaborating-vs-objects branch January 2, 2020 21:14
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.

Experiment with elaborating member-wise when assigning to union types
6 participants