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

Control flow analysis for destructured discriminated unions #46266

Merged
merged 5 commits into from
Nov 2, 2021
Merged

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Oct 7, 2021

This PR implements control flow analysis for dependent parameters and variables declared by destructuring discriminated unions. Specifically, when non-rest binding elements are declared as const variables or const-like parameters (parameters for which there are no assignments in the function body) and the parent type for the destructuring is a discriminated union type, conditional checks for variables destructured from discriminant properties now affect the types of other variables declared in the same destructuring.

Some examples:

type Action =
    | { kind: 'A', payload: number }
    | { kind: 'B', payload: string };

function f10({ kind, payload }: Action) {
    if (kind === 'A') {
        payload.toFixed();
    }
    if (kind === 'B') {
        payload.toUpperCase();
    }
}

function f11(action: Action) {
    const { kind, payload } = action;
    if (kind === 'A') {
        payload.toFixed();
    }
    if (kind === 'B') {
        payload.toUpperCase();
    }
}

function f12({ kind, payload }: Action) {
    switch (kind) {
        case 'A':
            payload.toFixed();
            break;
        case 'B':
            payload.toUpperCase();
            break;
        default:
            payload;  // never
    }
}

function f13(it: Iterator<number>) {
    const { value, done } = it.next();
    if (!done) {
        value;  // number
    }
}

Fixes #10830.
Fixes #35283.
Fixes #38020.
Fixes #46143.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Oct 7, 2021
@DanielRosenwasser
Copy link
Member

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @DanielRosenwasser, I've started to run the extended test suite on this PR at dd07cdc. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at dd07cdc. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @DanielRosenwasser, I've started to run the parallelized community code test suite on this PR at dd07cdc. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @DanielRosenwasser, I've started to run the perf test suite on this PR at dd07cdc. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @DanielRosenwasser, I've started to run the parallelized Definitely Typed test suite on this PR at dd07cdc. You can monitor the build here.

@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.

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..46266

Metric main 46266 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 354,215k (± 0.03%) 354,186k (± 0.01%) -29k (- 0.01%) 354,087k 354,305k
Parse Time 1.96s (± 0.53%) 1.96s (± 0.71%) +0.00s (+ 0.05%) 1.93s 1.99s
Bind Time 0.84s (± 0.95%) 0.84s (± 0.90%) +0.01s (+ 0.84%) 0.83s 0.86s
Check Time 5.46s (± 0.35%) 5.48s (± 0.45%) +0.03s (+ 0.48%) 5.45s 5.56s
Emit Time 5.87s (± 0.62%) 5.86s (± 0.57%) -0.00s (- 0.05%) 5.79s 5.97s
Total Time 14.12s (± 0.27%) 14.16s (± 0.36%) +0.04s (+ 0.25%) 14.05s 14.27s
Compiler-Unions - node (v10.16.3, x64)
Memory used 203,893k (± 0.03%) 203,892k (± 0.02%) -1k (- 0.00%) 203,797k 203,997k
Parse Time 0.78s (± 0.63%) 0.79s (± 0.84%) +0.01s (+ 1.15%) 0.78s 0.81s
Bind Time 0.52s (± 1.19%) 0.52s (± 1.39%) +0.00s (+ 0.58%) 0.51s 0.54s
Check Time 7.88s (± 0.53%) 7.96s (± 0.90%) +0.08s (+ 1.05%) 7.79s 8.15s
Emit Time 2.44s (± 1.14%) 2.45s (± 0.70%) +0.01s (+ 0.33%) 2.42s 2.50s
Total Time 11.63s (± 0.46%) 11.73s (± 0.58%) +0.10s (+ 0.89%) 11.56s 11.91s
Monaco - node (v10.16.3, x64)
Memory used 342,126k (± 0.03%) 342,090k (± 0.02%) -36k (- 0.01%) 341,983k 342,353k
Parse Time 1.48s (± 0.58%) 1.48s (± 0.73%) -0.00s (- 0.20%) 1.45s 1.50s
Bind Time 0.74s (± 0.64%) 0.75s (± 0.30%) +0.01s (+ 0.81%) 0.74s 0.75s
Check Time 5.44s (± 0.50%) 5.46s (± 0.70%) +0.02s (+ 0.44%) 5.40s 5.56s
Emit Time 3.20s (± 0.57%) 3.16s (± 0.65%) -0.04s (- 1.25%) 3.11s 3.22s
Total Time 10.87s (± 0.36%) 10.85s (± 0.32%) -0.02s (- 0.14%) 10.75s 10.92s
TFS - node (v10.16.3, x64)
Memory used 304,810k (± 0.02%) 304,836k (± 0.02%) +26k (+ 0.01%) 304,675k 304,993k
Parse Time 1.19s (± 0.54%) 1.20s (± 0.72%) +0.01s (+ 0.84%) 1.19s 1.22s
Bind Time 0.71s (± 0.87%) 0.71s (± 0.87%) -0.00s (- 0.28%) 0.69s 0.72s
Check Time 4.98s (± 0.49%) 4.99s (± 0.44%) +0.01s (+ 0.24%) 4.95s 5.03s
Emit Time 3.31s (± 1.01%) 3.36s (± 1.92%) +0.05s (+ 1.39%) 3.28s 3.60s
Total Time 10.20s (± 0.47%) 10.26s (± 0.76%) +0.07s (+ 0.65%) 10.13s 10.52s
material-ui - node (v10.16.3, x64)
Memory used 472,371k (± 0.01%) 472,446k (± 0.02%) +75k (+ 0.02%) 472,275k 472,678k
Parse Time 1.77s (± 0.38%) 1.78s (± 0.41%) +0.01s (+ 0.62%) 1.76s 1.79s
Bind Time 0.66s (± 0.79%) 0.66s (± 0.55%) -0.00s (- 0.46%) 0.65s 0.66s
Check Time 14.38s (± 0.27%) 14.49s (± 0.66%) +0.11s (+ 0.74%) 14.32s 14.77s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.81s (± 0.22%) 16.93s (± 0.59%) +0.12s (+ 0.68%) 16.75s 17.22s
Angular - node (v12.1.0, x64)
Memory used 332,133k (± 0.02%) 332,132k (± 0.02%) -1k (- 0.00%) 332,036k 332,241k
Parse Time 1.94s (± 0.88%) 1.96s (± 0.55%) +0.02s (+ 1.13%) 1.94s 1.98s
Bind Time 0.81s (± 0.64%) 0.83s (± 1.29%) +0.02s (+ 1.98%) 0.80s 0.86s
Check Time 5.28s (± 0.29%) 5.34s (± 0.60%) +0.06s (+ 1.10%) 5.27s 5.39s
Emit Time 6.12s (± 0.83%) 6.12s (± 0.68%) +0.01s (+ 0.10%) 6.02s 6.23s
Total Time 14.15s (± 0.41%) 14.25s (± 0.54%) +0.10s (+ 0.69%) 14.07s 14.36s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,377k (± 0.03%) 191,369k (± 0.08%) -8k (- 0.00%) 190,745k 191,518k
Parse Time 0.78s (± 0.75%) 0.79s (± 0.82%) +0.01s (+ 1.42%) 0.77s 0.80s
Bind Time 0.53s (± 0.71%) 0.53s (± 0.76%) +0.00s (+ 0.76%) 0.52s 0.54s
Check Time 7.35s (± 0.49%) 7.43s (± 0.57%) +0.08s (+ 1.09%) 7.34s 7.53s
Emit Time 2.47s (± 0.69%) 2.46s (± 0.77%) -0.00s (- 0.16%) 2.41s 2.50s
Total Time 11.12s (± 0.42%) 11.21s (± 0.49%) +0.09s (+ 0.81%) 11.12s 11.35s
Monaco - node (v12.1.0, x64)
Memory used 325,207k (± 0.03%) 325,124k (± 0.03%) -82k (- 0.03%) 324,858k 325,256k
Parse Time 1.46s (± 0.73%) 1.47s (± 0.56%) +0.00s (+ 0.07%) 1.45s 1.49s
Bind Time 0.73s (± 0.45%) 0.73s (± 0.76%) +0.00s (+ 0.27%) 0.72s 0.75s
Check Time 5.33s (± 0.60%) 5.34s (± 0.43%) +0.02s (+ 0.28%) 5.29s 5.38s
Emit Time 3.19s (± 0.87%) 3.19s (± 1.05%) +0.01s (+ 0.25%) 3.13s 3.28s
Total Time 10.71s (± 0.48%) 10.74s (± 0.42%) +0.03s (+ 0.26%) 10.67s 10.85s
TFS - node (v12.1.0, x64)
Memory used 289,482k (± 0.02%) 289,479k (± 0.02%) -3k (- 0.00%) 289,379k 289,609k
Parse Time 1.21s (± 0.74%) 1.23s (± 0.87%) +0.01s (+ 0.82%) 1.21s 1.25s
Bind Time 0.69s (± 0.83%) 0.69s (± 0.69%) +0.00s (+ 0.43%) 0.68s 0.70s
Check Time 4.91s (± 0.64%) 4.91s (± 0.30%) +0.01s (+ 0.14%) 4.88s 4.95s
Emit Time 3.36s (± 0.86%) 3.36s (± 1.15%) +0.00s (+ 0.06%) 3.29s 3.47s
Total Time 10.17s (± 0.44%) 10.19s (± 0.54%) +0.02s (+ 0.24%) 10.09s 10.32s
material-ui - node (v12.1.0, x64)
Memory used 450,968k (± 0.05%) 451,115k (± 0.01%) +147k (+ 0.03%) 451,009k 451,259k
Parse Time 1.77s (± 0.60%) 1.79s (± 0.71%) +0.01s (+ 0.79%) 1.75s 1.82s
Bind Time 0.64s (± 0.87%) 0.64s (± 1.18%) +0.00s (+ 0.47%) 0.63s 0.66s
Check Time 12.95s (± 0.37%) 13.11s (± 0.62%) +0.16s (+ 1.27%) 12.91s 13.30s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.37s (± 0.35%) 15.55s (± 0.58%) +0.18s (+ 1.15%) 15.32s 15.74s
Angular - node (v14.15.1, x64)
Memory used 330,493k (± 0.01%) 330,531k (± 0.01%) +38k (+ 0.01%) 330,483k 330,588k
Parse Time 1.94s (± 0.77%) 1.95s (± 0.55%) +0.01s (+ 0.31%) 1.93s 1.98s
Bind Time 0.86s (± 0.55%) 0.86s (± 0.61%) +0.00s (+ 0.23%) 0.85s 0.87s
Check Time 5.36s (± 0.69%) 5.41s (± 0.52%) +0.05s (+ 0.95%) 5.34s 5.45s
Emit Time 6.21s (± 0.84%) 6.26s (± 0.73%) +0.05s (+ 0.81%) 6.15s 6.38s
Total Time 14.37s (± 0.48%) 14.48s (± 0.43%) +0.11s (+ 0.77%) 14.30s 14.57s
Compiler-Unions - node (v14.15.1, x64)
Memory used 193,198k (± 0.02%) 192,904k (± 0.37%) -293k (- 0.15%) 190,010k 193,286k
Parse Time 0.80s (± 0.62%) 0.81s (± 0.64%) +0.01s (+ 0.75%) 0.80s 0.82s
Bind Time 0.55s (± 0.54%) 0.56s (± 0.84%) +0.01s (+ 1.45%) 0.55s 0.57s
Check Time 7.54s (± 0.65%) 7.60s (± 0.77%) +0.06s (+ 0.85%) 7.45s 7.76s
Emit Time 2.44s (± 0.95%) 2.45s (± 0.47%) +0.01s (+ 0.53%) 2.43s 2.48s
Total Time 11.34s (± 0.59%) 11.42s (± 0.58%) +0.09s (+ 0.77%) 11.26s 11.59s
Monaco - node (v14.15.1, x64)
Memory used 323,993k (± 0.01%) 323,996k (± 0.01%) +4k (+ 0.00%) 323,961k 324,042k
Parse Time 1.51s (± 0.55%) 1.52s (± 0.53%) +0.01s (+ 0.86%) 1.50s 1.54s
Bind Time 0.76s (± 0.65%) 0.76s (± 0.58%) +0.01s (+ 0.79%) 0.75s 0.77s
Check Time 5.29s (± 0.28%) 5.34s (± 0.44%) +0.04s (+ 0.83%) 5.27s 5.40s
Emit Time 3.22s (± 0.69%) 3.24s (± 0.52%) +0.01s (+ 0.40%) 3.20s 3.27s
Total Time 10.78s (± 0.30%) 10.85s (± 0.23%) +0.08s (+ 0.72%) 10.81s 10.93s
TFS - node (v14.15.1, x64)
Memory used 288,362k (± 0.01%) 288,344k (± 0.01%) -18k (- 0.01%) 288,291k 288,400k
Parse Time 1.23s (± 0.54%) 1.24s (± 0.63%) +0.01s (+ 0.73%) 1.22s 1.26s
Bind Time 0.73s (± 0.61%) 0.74s (± 0.68%) +0.00s (+ 0.41%) 0.72s 0.74s
Check Time 4.91s (± 0.46%) 4.96s (± 0.43%) +0.05s (+ 1.08%) 4.91s 5.00s
Emit Time 3.47s (± 0.65%) 3.51s (± 0.79%) +0.04s (+ 1.24%) 3.47s 3.58s
Total Time 10.34s (± 0.37%) 10.44s (± 0.48%) +0.11s (+ 1.04%) 10.34s 10.57s
material-ui - node (v14.15.1, x64)
Memory used 449,073k (± 0.08%) 449,399k (± 0.00%) +326k (+ 0.07%) 449,363k 449,460k
Parse Time 1.81s (± 0.43%) 1.83s (± 0.32%) +0.02s (+ 1.38%) 1.82s 1.85s
Bind Time 0.68s (± 0.70%) 0.68s (± 0.53%) +0.01s (+ 1.03%) 0.68s 0.69s
Check Time 13.14s (± 1.08%) 13.32s (± 0.73%) +0.18s (+ 1.34%) 13.05s 13.53s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.63s (± 0.92%) 15.84s (± 0.60%) +0.21s (+ 1.32%) 15.57s 16.03s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory8 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 46266 10
Baseline main 10

Developer Information:

Download Benchmark

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Oct 8, 2021
@ahejlsberg
Copy link
Member Author

@typescript-bot perf test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @ahejlsberg, I've started to run the perf test suite on this PR at 76fbe25. You can monitor the build here.

Update: The results are in!

const declaration = symbol.valueDeclaration;
if (declaration && isBindingElement(declaration) && !declaration.initializer && !declaration.dotDotDotToken && declaration.parent.elements.length >= 2) {
const parent = declaration.parent.parent;
if (parent.kind === SyntaxKind.VariableDeclaration && getCombinedNodeFlags(declaration) && NodeFlags.Const || parent.kind === SyntaxKind.Parameter) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Small typo here:

Suggested change
if (parent.kind === SyntaxKind.VariableDeclaration && getCombinedNodeFlags(declaration) && NodeFlags.Const || parent.kind === SyntaxKind.Parameter) {
if (parent.kind === SyntaxKind.VariableDeclaration && getCombinedNodeFlags(declaration) & NodeFlags.Const || parent.kind === SyntaxKind.Parameter) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good catch!

@ahejlsberg
Copy link
Member Author

@typescript-bot perf test this faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at cf546e8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 8, 2021

Heya @ahejlsberg, I've started to run the perf test suite on this PR at cf546e8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..46266

Metric main 46266 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 354,210k (± 0.02%) 354,233k (± 0.02%) +24k (+ 0.01%) 354,043k 354,385k
Parse Time 1.95s (± 0.34%) 1.94s (± 0.43%) -0.00s (- 0.10%) 1.93s 1.97s
Bind Time 0.84s (± 1.00%) 0.84s (± 0.58%) +0.00s (+ 0.36%) 0.83s 0.85s
Check Time 5.45s (± 0.31%) 5.47s (± 0.35%) +0.02s (+ 0.44%) 5.43s 5.51s
Emit Time 5.85s (± 0.43%) 5.85s (± 0.76%) -0.00s (- 0.07%) 5.76s 5.97s
Total Time 14.09s (± 0.19%) 14.11s (± 0.47%) +0.02s (+ 0.15%) 13.96s 14.30s
Compiler-Unions - node (v10.16.3, x64)
Memory used 203,833k (± 0.04%) 203,809k (± 0.04%) -24k (- 0.01%) 203,681k 204,026k
Parse Time 0.79s (± 0.71%) 0.79s (± 0.98%) +0.00s (+ 0.13%) 0.77s 0.80s
Bind Time 0.52s (± 1.54%) 0.52s (± 1.56%) +0.01s (+ 0.97%) 0.50s 0.54s
Check Time 7.87s (± 0.57%) 7.95s (± 0.68%) +0.08s (+ 0.95%) 7.82s 8.04s
Emit Time 2.45s (± 1.43%) 2.46s (± 1.10%) +0.01s (+ 0.45%) 2.41s 2.52s
Total Time 11.64s (± 0.53%) 11.72s (± 0.58%) +0.09s (+ 0.74%) 11.59s 11.83s
Monaco - node (v10.16.3, x64)
Memory used 342,076k (± 0.02%) 342,109k (± 0.03%) +33k (+ 0.01%) 341,936k 342,287k
Parse Time 1.49s (± 0.24%) 1.48s (± 0.61%) -0.01s (- 0.47%) 1.45s 1.49s
Bind Time 0.75s (± 0.66%) 0.75s (± 0.64%) +0.00s (+ 0.13%) 0.73s 0.75s
Check Time 5.44s (± 0.79%) 5.46s (± 0.67%) +0.02s (+ 0.42%) 5.34s 5.52s
Emit Time 3.17s (± 0.59%) 3.18s (± 1.10%) +0.01s (+ 0.28%) 3.08s 3.25s
Total Time 10.84s (± 0.30%) 10.86s (± 0.45%) +0.02s (+ 0.22%) 10.70s 10.93s
TFS - node (v10.16.3, x64)
Memory used 304,778k (± 0.02%) 304,852k (± 0.02%) +73k (+ 0.02%) 304,693k 304,958k
Parse Time 1.20s (± 0.54%) 1.20s (± 1.06%) +0.00s (+ 0.33%) 1.18s 1.23s
Bind Time 0.71s (± 1.31%) 0.71s (± 0.81%) +0.00s (+ 0.71%) 0.70s 0.73s
Check Time 4.97s (± 0.54%) 5.02s (± 0.36%) +0.05s (+ 0.97%) 4.98s 5.06s
Emit Time 3.32s (± 1.15%) 3.32s (± 0.76%) +0.01s (+ 0.18%) 3.27s 3.40s
Total Time 10.19s (± 0.47%) 10.25s (± 0.36%) +0.06s (+ 0.61%) 10.16s 10.35s
material-ui - node (v10.16.3, x64)
Memory used 472,372k (± 0.02%) 472,383k (± 0.01%) +11k (+ 0.00%) 472,258k 472,497k
Parse Time 1.77s (± 0.55%) 1.77s (± 0.65%) +0.00s (+ 0.06%) 1.75s 1.80s
Bind Time 0.65s (± 0.56%) 0.66s (± 0.87%) +0.01s (+ 1.38%) 0.65s 0.68s
Check Time 14.41s (± 0.32%) 14.52s (± 0.44%) +0.10s (+ 0.69%) 14.36s 14.68s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.84s (± 0.28%) 16.95s (± 0.40%) +0.11s (+ 0.65%) 16.80s 17.12s
Angular - node (v12.1.0, x64)
Memory used 332,068k (± 0.02%) 332,158k (± 0.02%) +90k (+ 0.03%) 331,986k 332,291k
Parse Time 1.94s (± 0.80%) 1.95s (± 0.75%) +0.01s (+ 0.67%) 1.92s 2.00s
Bind Time 0.82s (± 0.82%) 0.82s (± 0.60%) -0.00s (- 0.12%) 0.81s 0.83s
Check Time 5.28s (± 0.41%) 5.33s (± 0.51%) +0.05s (+ 0.87%) 5.27s 5.39s
Emit Time 6.10s (± 0.55%) 6.17s (± 1.04%) +0.07s (+ 1.18%) 6.03s 6.35s
Total Time 14.13s (± 0.36%) 14.26s (± 0.60%) +0.13s (+ 0.91%) 14.06s 14.45s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,388k (± 0.04%) 191,346k (± 0.09%) -42k (- 0.02%) 190,687k 191,554k
Parse Time 0.78s (± 0.93%) 0.78s (± 0.85%) +0.00s (+ 0.13%) 0.77s 0.80s
Bind Time 0.53s (± 1.27%) 0.53s (± 1.25%) +0.01s (+ 1.14%) 0.52s 0.55s
Check Time 7.36s (± 0.84%) 7.45s (± 0.47%) +0.10s (+ 1.29%) 7.39s 7.55s
Emit Time 2.45s (± 0.73%) 2.46s (± 0.85%) +0.00s (+ 0.16%) 2.41s 2.51s
Total Time 11.12s (± 0.65%) 11.23s (± 0.29%) +0.11s (+ 0.94%) 11.16s 11.29s
Monaco - node (v12.1.0, x64)
Memory used 325,154k (± 0.02%) 325,110k (± 0.06%) -44k (- 0.01%) 324,311k 325,313k
Parse Time 1.46s (± 0.65%) 1.48s (± 0.71%) +0.01s (+ 0.82%) 1.46s 1.50s
Bind Time 0.72s (± 0.50%) 0.73s (± 0.61%) +0.00s (+ 0.55%) 0.72s 0.74s
Check Time 5.34s (± 0.28%) 5.38s (± 0.48%) +0.03s (+ 0.62%) 5.31s 5.42s
Emit Time 3.20s (± 0.39%) 3.20s (± 1.40%) +0.00s (+ 0.09%) 3.14s 3.34s
Total Time 10.73s (± 0.15%) 10.79s (± 0.49%) +0.05s (+ 0.49%) 10.72s 10.97s
TFS - node (v12.1.0, x64)
Memory used 289,435k (± 0.02%) 289,512k (± 0.01%) +77k (+ 0.03%) 289,449k 289,621k
Parse Time 1.22s (± 0.96%) 1.22s (± 0.83%) -0.01s (- 0.49%) 1.19s 1.23s
Bind Time 0.69s (± 0.80%) 0.69s (± 1.09%) +0.00s (+ 0.43%) 0.68s 0.72s
Check Time 4.89s (± 0.43%) 4.95s (± 0.54%) +0.06s (+ 1.31%) 4.88s 4.99s
Emit Time 3.35s (± 0.86%) 3.39s (± 0.93%) +0.04s (+ 1.22%) 3.32s 3.46s
Total Time 10.15s (± 0.43%) 10.25s (± 0.51%) +0.10s (+ 0.99%) 10.14s 10.38s
material-ui - node (v12.1.0, x64)
Memory used 451,082k (± 0.02%) 451,118k (± 0.02%) +36k (+ 0.01%) 450,953k 451,256k
Parse Time 1.78s (± 0.53%) 1.78s (± 0.67%) +0.00s (+ 0.17%) 1.75s 1.81s
Bind Time 0.64s (± 1.14%) 0.65s (± 1.39%) +0.01s (+ 1.10%) 0.63s 0.67s
Check Time 12.95s (± 0.57%) 13.06s (± 0.55%) +0.11s (+ 0.83%) 12.92s 13.28s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.37s (± 0.47%) 15.49s (± 0.52%) +0.12s (+ 0.78%) 15.32s 15.73s
Angular - node (v14.15.1, x64)
Memory used 330,505k (± 0.00%) 330,510k (± 0.00%) +6k (+ 0.00%) 330,485k 330,541k
Parse Time 1.94s (± 0.56%) 1.95s (± 0.76%) +0.01s (+ 0.62%) 1.92s 1.99s
Bind Time 0.86s (± 0.87%) 0.86s (± 0.67%) +0.00s (+ 0.35%) 0.85s 0.87s
Check Time 5.34s (± 0.44%) 5.40s (± 0.37%) +0.05s (+ 1.01%) 5.35s 5.44s
Emit Time 6.17s (± 0.69%) 6.22s (± 0.72%) +0.05s (+ 0.78%) 6.14s 6.36s
Total Time 14.31s (± 0.39%) 14.43s (± 0.39%) +0.12s (+ 0.82%) 14.32s 14.59s
Compiler-Unions - node (v14.15.1, x64)
Memory used 192,887k (± 0.37%) 192,859k (± 0.36%) -28k (- 0.01%) 190,053k 193,271k
Parse Time 0.81s (± 0.45%) 0.81s (± 0.71%) +0.00s (+ 0.50%) 0.80s 0.83s
Bind Time 0.55s (± 0.66%) 0.56s (± 1.03%) +0.01s (+ 1.08%) 0.55s 0.57s
Check Time 7.53s (± 0.56%) 7.59s (± 0.58%) +0.06s (+ 0.76%) 7.51s 7.70s
Emit Time 2.46s (± 0.92%) 2.47s (± 0.95%) +0.01s (+ 0.33%) 2.41s 2.52s
Total Time 11.35s (± 0.48%) 11.43s (± 0.48%) +0.08s (+ 0.66%) 11.29s 11.57s
Monaco - node (v14.15.1, x64)
Memory used 324,019k (± 0.00%) 324,007k (± 0.01%) -11k (- 0.00%) 323,961k 324,051k
Parse Time 1.50s (± 0.35%) 1.51s (± 0.85%) +0.01s (+ 0.47%) 1.49s 1.55s
Bind Time 0.76s (± 0.48%) 0.76s (± 1.11%) +0.00s (+ 0.40%) 0.75s 0.79s
Check Time 5.31s (± 0.63%) 5.32s (± 0.46%) +0.01s (+ 0.17%) 5.26s 5.37s
Emit Time 3.21s (± 0.89%) 3.24s (± 0.69%) +0.03s (+ 0.97%) 3.19s 3.28s
Total Time 10.79s (± 0.51%) 10.83s (± 0.45%) +0.05s (+ 0.44%) 10.71s 10.91s
TFS - node (v14.15.1, x64)
Memory used 288,348k (± 0.01%) 288,347k (± 0.00%) -1k (- 0.00%) 288,322k 288,362k
Parse Time 1.24s (± 0.52%) 1.23s (± 0.50%) -0.01s (- 0.73%) 1.22s 1.24s
Bind Time 0.73s (± 0.71%) 0.73s (± 0.41%) +0.00s (+ 0.14%) 0.73s 0.74s
Check Time 4.91s (± 0.62%) 4.94s (± 0.48%) +0.03s (+ 0.61%) 4.88s 4.99s
Emit Time 3.49s (± 0.57%) 3.48s (± 0.98%) -0.01s (- 0.43%) 3.40s 3.56s
Total Time 10.37s (± 0.40%) 10.37s (± 0.52%) +0.01s (+ 0.08%) 10.26s 10.50s
material-ui - node (v14.15.1, x64)
Memory used 449,122k (± 0.06%) 449,271k (± 0.06%) +149k (+ 0.03%) 448,213k 449,436k
Parse Time 1.81s (± 0.48%) 1.82s (± 0.34%) +0.01s (+ 0.50%) 1.81s 1.84s
Bind Time 0.68s (± 0.85%) 0.68s (± 0.59%) +0.00s (+ 0.15%) 0.67s 0.69s
Check Time 13.16s (± 0.89%) 13.28s (± 0.63%) +0.11s (+ 0.87%) 13.10s 13.44s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.65s (± 0.77%) 15.78s (± 0.51%) +0.13s (+ 0.80%) 15.59s 15.94s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory8 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 46266 10
Baseline main 10

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..46266

Metric main 46266 Delta Best Worst
Angular - node (v14.15.1, x64)
Memory used 330,505k (± 0.00%) 330,506k (± 0.00%) +2k (+ 0.00%) 330,466k 330,542k
Parse Time 1.94s (± 0.56%) 1.99s (± 5.05%) +0.04s (+ 2.32%) 1.92s 2.39s
Bind Time 0.86s (± 0.87%) 0.86s (± 0.75%) +0.00s (+ 0.12%) 0.84s 0.87s
Check Time 5.34s (± 0.44%) 5.41s (± 0.31%) +0.07s (+ 1.25%) 5.37s 5.45s
Emit Time 6.17s (± 0.69%) 6.20s (± 0.70%) +0.04s (+ 0.58%) 6.11s 6.30s
Total Time 14.31s (± 0.39%) 14.46s (± 0.75%) +0.15s (+ 1.04%) 14.33s 14.86s
Compiler-Unions - node (v14.15.1, x64)
Memory used 192,887k (± 0.37%) 193,207k (± 0.03%) +320k (+ 0.17%) 193,084k 193,294k
Parse Time 0.81s (± 0.45%) 0.81s (± 0.64%) +0.00s (+ 0.37%) 0.80s 0.82s
Bind Time 0.55s (± 0.66%) 0.56s (± 0.67%) +0.00s (+ 0.18%) 0.55s 0.56s
Check Time 7.53s (± 0.56%) 7.56s (± 0.73%) +0.03s (+ 0.40%) 7.44s 7.73s
Emit Time 2.46s (± 0.92%) 2.46s (± 1.16%) +0.01s (+ 0.33%) 2.42s 2.56s
Total Time 11.35s (± 0.48%) 11.39s (± 0.47%) +0.04s (+ 0.37%) 11.30s 11.54s
Monaco - node (v14.15.1, x64)
Memory used 324,019k (± 0.00%) 323,991k (± 0.00%) -28k (- 0.01%) 323,965k 324,031k
Parse Time 1.50s (± 0.35%) 1.51s (± 1.25%) +0.00s (+ 0.27%) 1.49s 1.58s
Bind Time 0.76s (± 0.48%) 0.76s (± 0.65%) -0.00s (- 0.00%) 0.75s 0.77s
Check Time 5.31s (± 0.63%) 5.29s (± 0.38%) -0.03s (- 0.51%) 5.24s 5.33s
Emit Time 3.21s (± 0.89%) 3.21s (± 1.03%) -0.00s (- 0.12%) 3.14s 3.28s
Total Time 10.79s (± 0.51%) 10.76s (± 0.39%) -0.03s (- 0.27%) 10.66s 10.85s
TFS - node (v14.15.1, x64)
Memory used 288,348k (± 0.01%) 288,341k (± 0.01%) -7k (- 0.00%) 288,291k 288,396k
Parse Time 1.24s (± 0.52%) 1.24s (± 1.07%) +0.00s (+ 0.16%) 1.22s 1.28s
Bind Time 0.73s (± 0.71%) 0.73s (± 0.93%) +0.00s (+ 0.41%) 0.72s 0.75s
Check Time 4.91s (± 0.62%) 4.92s (± 0.44%) +0.01s (+ 0.18%) 4.85s 4.95s
Emit Time 3.49s (± 0.57%) 3.47s (± 0.70%) -0.02s (- 0.60%) 3.42s 3.52s
Total Time 10.37s (± 0.40%) 10.36s (± 0.44%) -0.01s (- 0.09%) 10.26s 10.43s
material-ui - node (v14.15.1, x64)
Memory used 449,122k (± 0.06%) 449,323k (± 0.04%) +200k (+ 0.04%) 448,607k 449,447k
Parse Time 1.81s (± 0.48%) 1.83s (± 2.09%) +0.02s (+ 1.05%) 1.79s 1.98s
Bind Time 0.68s (± 0.85%) 0.68s (± 0.76%) +0.00s (+ 0.15%) 0.67s 0.69s
Check Time 13.16s (± 0.89%) 13.24s (± 0.97%) +0.08s (+ 0.60%) 13.03s 13.51s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.65s (± 0.77%) 15.75s (± 0.78%) +0.10s (+ 0.63%) 15.51s 16.02s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory6 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 46266 10
Baseline main 10

Developer Information:

Download Benchmark

@athyuttamre
Copy link

This is super exciting, thanks for working on this @ahejlsberg! I just ran into this today when working with Result types.

mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
…t#46266)

* CFA for dependent variables destructured from discriminated union

* Accept new baselines

* Add tests

* Limit calls to isSymbolAssigned

* Fix wrong operator
@slorber
Copy link

slorber commented Jan 25, 2022

@DanielRosenwasser maybe it's worth adding this feature to the 4.6 beta blog post:

https://devblogs.microsoft.com/typescript/announcing-typescript-4-6-beta/#control-flow-analysis-for-dependent-parameters

The blog post does not mention object restructuring, only tuple/union destructuring, but I checked the 4.6 nightly playground and it works with objects too now, which is highly anticipated by React users (more than the example showcased in the blog post IMHO)


Edit: oups actually I'm wrong it's not yet 100% good enough for React, and the following still fails:

import React from "react";

type ItemData =
    | { kind: 'user', name: string }
    | { kind: 'company', id: string }

function Item({ kind, ...data }: ItemData) {
    if (kind === 'user') {
        <div>{data.id}</div>
    }
    if (kind === 'company') {
        <div>{data.name}</div>
    }
    return null;
}

But the following now works:

type ItemData2 =
    | { kind: 'user', data: {name: string} }
    | { kind: 'company', data: {id: string} }

function Item2({ kind, data }: ItemData2) {
    if (kind === 'user') {
        <div>{data.name}</div>
    }
    if (kind === 'company') {
        <div>{data.id}</div>
    }
    return null;
}

Still worth highlighting that in the post :)

@DanielRosenwasser
Copy link
Member

Seems like the difference there is the spread, right?

Okay, that's a good call, we must have missed that. I will likely add that to the RC post.

@DawChihLiou
Copy link

Awesome! Thank you so much❤️

@luxalpa
Copy link

luxalpa commented Mar 14, 2022

Just a question of understanding: Is the support for the spread syntax planned for a bugfix release or for a minor version? Or is it currently not being planned to be changed / given priority?

@slorber
Copy link

slorber commented Mar 24, 2022

@luxalpa tracked here: #46680

Would also like to see this implemented 😄

@50an6xy06r6n
Copy link

I've noticed that this feature doesn't seem to play nice with default values:

type T = {
	flag: true;
	a: number;
} | {
	flag: false;
	a: string;
}

// This code works if you remove the "= true"
({ flag = true, a }: T) => {
	if (flag) {
		a.toFixed(); // thinks it can be either a string or number again
	} else {
		a.toUpperCase(); // same as above
	}
}

Is this also covered by #46680 or is there another reason it doesn't work?

@RyanCavanaugh
Copy link
Member

@50an6xy06r6n that feels like a bug

@ahejlsberg
Copy link
Member Author

It's a design limitation, but one we could consider removing. We only do CFA for dependent destructured parameters when they're declared without initializers because it adds complexity to consider the contribution the initializer might make to the inferred declared type of the parameter.

@50an6xy06r6n
Copy link

@ahejlsberg intuitively it feels like it should work. In my case (react), the way props work means you have to pass parameters as an object, though I realized that there's other ways to set the default value.

@josh-leyshon
Copy link

This also does not work when destructuring a nested discriminated union type. See playground link here.

I assume this is because of this line in the PR description:

and the parent type for the destructuring is a discriminated union type

Since the object is nested, the parent type being destructured is not the discriminated union. Still, it's a shame this does not work.

@margaretdax
Copy link

I am currently running into issues with this (or a related) analysis. I have a union [A, B] | [C, D] | [E, F] and functions isA() isC() isE() and regardless of destructuring or not, narrowing the first tuple member cannot narrow the second tuple member.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone