Skip to content

Narrow falsy strings to '' and falsy numbers to 0 #45836

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

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Sep 12, 2021

The existing filterType function used by getTypeWithFacts <- narrowTypeByTruthiness works as a filter: taking in an existing set of types and returning the ones that are allowed. This PR sets up a filterAndNarrowType function that can also transform the types given to it into more narrow equivalents. truthyTypeFilter is then used by narrowTypeByTruthiness to turns primitives into their falsy literals (e.g. number -> 0) when falsy.

Sending as a draft as requested so we can try this out on real world code. Opening as a full PR since it's been a bit and none of the typescript-bot commands have shown any major red flags.

Fixes #45329

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 12, 2021
@andrewbranch
Copy link
Member

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

Heya @andrewbranch, I've started to run the inline community code test suite on this PR at 86e00f6. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@andrewbranch
Great news! no new errors were found between main..refs/pull/45836/merge

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 13, 2021

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/110403/artifacts?artifactName=tgz&fileId=5601482CCA0B49C4D70883A12C1D2202B98046474A5AD3D45ECD70AFCCB5F3EF02&fileName=/typescript-4.5.0-insiders.20210913.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..45836

Metric main 45836 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 351,557k (± 0.02%) 351,527k (± 0.01%) -30k (- 0.01%) 351,424k 351,620k
Parse Time 1.91s (± 0.52%) 1.91s (± 0.70%) 0.00s ( 0.00%) 1.88s 1.94s
Bind Time 0.85s (± 0.85%) 0.86s (± 0.67%) +0.01s (+ 0.94%) 0.84s 0.87s
Check Time 5.46s (± 0.51%) 5.46s (± 0.44%) 0.00s ( 0.00%) 5.41s 5.53s
Emit Time 5.83s (± 0.50%) 5.86s (± 0.53%) +0.03s (+ 0.57%) 5.80s 5.92s
Total Time 14.06s (± 0.32%) 14.10s (± 0.39%) +0.04s (+ 0.28%) 13.98s 14.20s
Compiler-Unions - node (v10.16.3, x64)
Memory used 203,516k (± 0.02%) 203,581k (± 0.03%) +65k (+ 0.03%) 203,445k 203,666k
Parse Time 0.78s (± 0.43%) 0.79s (± 1.05%) +0.01s (+ 0.77%) 0.77s 0.80s
Bind Time 0.54s (± 0.93%) 0.53s (± 1.23%) -0.01s (- 1.31%) 0.52s 0.54s
Check Time 7.92s (± 0.59%) 7.97s (± 0.82%) +0.04s (+ 0.56%) 7.83s 8.17s
Emit Time 2.44s (± 0.49%) 2.46s (± 0.71%) +0.02s (+ 0.90%) 2.42s 2.51s
Total Time 11.67s (± 0.39%) 11.74s (± 0.66%) +0.07s (+ 0.58%) 11.59s 11.96s
Monaco - node (v10.16.3, x64)
Memory used 340,661k (± 0.02%) 340,646k (± 0.01%) -15k (- 0.00%) 340,546k 340,756k
Parse Time 1.46s (± 1.13%) 1.46s (± 1.00%) -0.00s (- 0.14%) 1.42s 1.48s
Bind Time 0.75s (± 0.89%) 0.75s (± 0.79%) +0.00s (+ 0.27%) 0.74s 0.77s
Check Time 5.48s (± 0.60%) 5.43s (± 0.86%) -0.05s (- 0.93%) 5.32s 5.54s
Emit Time 3.17s (± 0.77%) 3.20s (± 1.12%) +0.03s (+ 0.95%) 3.12s 3.29s
Total Time 10.86s (± 0.55%) 10.84s (± 0.48%) -0.02s (- 0.17%) 10.75s 10.99s
TFS - node (v10.16.3, x64)
Memory used 304,025k (± 0.02%) 304,067k (± 0.02%) +42k (+ 0.01%) 303,932k 304,183k
Parse Time 1.19s (± 0.75%) 1.19s (± 0.83%) -0.01s (- 0.50%) 1.17s 1.22s
Bind Time 0.72s (± 0.51%) 0.72s (± 0.47%) -0.01s (- 1.10%) 0.71s 0.72s
Check Time 4.96s (± 0.70%) 4.96s (± 0.55%) +0.00s (+ 0.04%) 4.89s 5.03s
Emit Time 3.37s (± 1.04%) 3.35s (± 2.00%) -0.02s (- 0.47%) 3.22s 3.53s
Total Time 10.25s (± 0.53%) 10.22s (± 0.80%) -0.03s (- 0.25%) 10.05s 10.44s
material-ui - node (v10.16.3, x64)
Memory used 468,308k (± 0.01%) 468,326k (± 0.01%) +18k (+ 0.00%) 468,218k 468,413k
Parse Time 1.74s (± 0.43%) 1.73s (± 0.72%) -0.01s (- 0.34%) 1.71s 1.76s
Bind Time 0.67s (± 0.99%) 0.67s (± 1.03%) -0.01s (- 0.75%) 0.65s 0.68s
Check Time 14.43s (± 0.63%) 14.42s (± 0.59%) -0.01s (- 0.09%) 14.20s 14.55s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.84s (± 0.57%) 16.82s (± 0.57%) -0.02s (- 0.13%) 16.57s 16.99s
Angular - node (v12.1.0, x64)
Memory used 329,526k (± 0.03%) 329,526k (± 0.03%) -0k (- 0.00%) 329,354k 329,717k
Parse Time 1.90s (± 0.92%) 1.90s (± 0.65%) +0.00s (+ 0.05%) 1.86s 1.92s
Bind Time 0.85s (± 0.40%) 0.84s (± 0.53%) -0.00s (- 0.59%) 0.83s 0.85s
Check Time 5.26s (± 0.57%) 5.32s (± 0.50%) +0.05s (+ 0.97%) 5.26s 5.37s
Emit Time 6.09s (± 0.52%) 6.13s (± 0.47%) +0.04s (+ 0.69%) 6.10s 6.24s
Total Time 14.10s (± 0.45%) 14.19s (± 0.34%) +0.09s (+ 0.61%) 14.07s 14.31s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,070k (± 0.08%) 190,995k (± 0.10%) -75k (- 0.04%) 190,382k 191,236k
Parse Time 0.79s (± 0.76%) 0.78s (± 0.60%) -0.01s (- 0.64%) 0.77s 0.79s
Bind Time 0.53s (± 0.68%) 0.53s (± 0.64%) -0.00s (- 0.19%) 0.53s 0.54s
Check Time 7.42s (± 0.79%) 7.51s (± 0.42%) +0.09s (+ 1.20%) 7.43s 7.59s
Emit Time 2.48s (± 0.67%) 2.47s (± 0.97%) -0.01s (- 0.20%) 2.43s 2.56s
Total Time 11.21s (± 0.59%) 11.29s (± 0.32%) +0.08s (+ 0.70%) 11.22s 11.37s
Monaco - node (v12.1.0, x64)
Memory used 323,804k (± 0.02%) 323,846k (± 0.01%) +41k (+ 0.01%) 323,760k 323,945k
Parse Time 1.44s (± 0.73%) 1.43s (± 0.71%) -0.01s (- 0.42%) 1.40s 1.45s
Bind Time 0.73s (± 0.45%) 0.73s (± 0.61%) -0.00s (- 0.27%) 0.72s 0.74s
Check Time 5.33s (± 0.43%) 5.33s (± 0.58%) -0.01s (- 0.17%) 5.25s 5.39s
Emit Time 3.23s (± 1.02%) 3.21s (± 0.89%) -0.02s (- 0.65%) 3.15s 3.27s
Total Time 10.73s (± 0.51%) 10.70s (± 0.50%) -0.03s (- 0.30%) 10.57s 10.83s
TFS - node (v12.1.0, x64)
Memory used 288,818k (± 0.02%) 288,845k (± 0.03%) +27k (+ 0.01%) 288,678k 289,081k
Parse Time 1.22s (± 0.68%) 1.21s (± 0.83%) -0.00s (- 0.16%) 1.20s 1.25s
Bind Time 0.70s (± 0.63%) 0.70s (± 0.49%) -0.01s (- 0.71%) 0.69s 0.70s
Check Time 4.89s (± 0.37%) 4.90s (± 0.62%) +0.00s (+ 0.08%) 4.83s 4.95s
Emit Time 3.38s (± 1.29%) 3.41s (± 0.55%) +0.02s (+ 0.71%) 3.37s 3.45s
Total Time 10.19s (± 0.57%) 10.21s (± 0.48%) +0.02s (+ 0.23%) 10.12s 10.31s
material-ui - node (v12.1.0, x64)
Memory used 447,028k (± 0.07%) 446,809k (± 0.10%) -219k (- 0.05%) 445,828k 447,295k
Parse Time 1.74s (± 0.57%) 1.74s (± 0.53%) -0.00s (- 0.23%) 1.71s 1.76s
Bind Time 0.67s (± 0.56%) 0.66s (± 0.57%) -0.01s (- 1.50%) 0.65s 0.66s
Check Time 12.98s (± 0.69%) 13.06s (± 0.96%) +0.08s (+ 0.62%) 12.79s 13.39s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.38s (± 0.59%) 15.46s (± 0.80%) +0.07s (+ 0.47%) 15.21s 15.77s
Angular - node (v14.15.1, x64)
Memory used 328,010k (± 0.05%) 328,045k (± 0.00%) +36k (+ 0.01%) 328,027k 328,059k
Parse Time 1.92s (± 0.59%) 1.91s (± 0.47%) -0.00s (- 0.21%) 1.89s 1.93s
Bind Time 0.88s (± 0.91%) 0.88s (± 0.39%) +0.00s (+ 0.11%) 0.88s 0.89s
Check Time 5.35s (± 0.35%) 5.36s (± 0.39%) +0.00s (+ 0.07%) 5.30s 5.38s
Emit Time 6.24s (± 0.63%) 6.26s (± 0.41%) +0.02s (+ 0.34%) 6.22s 6.32s
Total Time 14.39s (± 0.43%) 14.41s (± 0.29%) +0.02s (+ 0.15%) 14.33s 14.51s
Compiler-Unions - node (v14.15.1, x64)
Memory used 192,200k (± 0.49%) 191,266k (± 0.62%) -935k (- 0.49%) 189,611k 192,889k
Parse Time 0.81s (± 0.76%) 0.81s (± 0.84%) +0.01s (+ 0.62%) 0.80s 0.83s
Bind Time 0.56s (± 0.71%) 0.56s (± 0.84%) -0.00s (- 0.18%) 0.55s 0.57s
Check Time 7.59s (± 0.68%) 7.67s (± 0.58%) +0.08s (+ 1.04%) 7.59s 7.81s
Emit Time 2.47s (± 1.03%) 2.44s (± 0.79%) -0.02s (- 0.89%) 2.41s 2.49s
Total Time 11.43s (± 0.49%) 11.49s (± 0.44%) +0.06s (+ 0.50%) 11.37s 11.63s
Monaco - node (v14.15.1, x64)
Memory used 322,564k (± 0.00%) 322,573k (± 0.00%) +9k (+ 0.00%) 322,556k 322,595k
Parse Time 1.49s (± 0.77%) 1.50s (± 0.59%) +0.00s (+ 0.27%) 1.48s 1.52s
Bind Time 0.76s (± 0.96%) 0.76s (± 0.63%) -0.00s (- 0.13%) 0.75s 0.77s
Check Time 5.30s (± 0.39%) 5.29s (± 0.48%) -0.01s (- 0.21%) 5.25s 5.37s
Emit Time 3.26s (± 0.79%) 3.27s (± 0.94%) +0.01s (+ 0.40%) 3.22s 3.37s
Total Time 10.81s (± 0.42%) 10.81s (± 0.47%) +0.00s (+ 0.04%) 10.72s 10.94s
TFS - node (v14.15.1, x64)
Memory used 287,743k (± 0.01%) 287,748k (± 0.01%) +6k (+ 0.00%) 287,696k 287,787k
Parse Time 1.25s (± 2.10%) 1.25s (± 1.29%) +0.00s (+ 0.32%) 1.21s 1.28s
Bind Time 0.74s (± 3.06%) 0.75s (± 6.10%) +0.01s (+ 1.48%) 0.71s 0.91s
Check Time 4.92s (± 0.38%) 4.91s (± 0.42%) -0.01s (- 0.18%) 4.87s 4.96s
Emit Time 3.48s (± 0.63%) 3.48s (± 0.82%) +0.00s (+ 0.09%) 3.41s 3.54s
Total Time 10.38s (± 0.35%) 10.39s (± 0.47%) +0.01s (+ 0.11%) 10.25s 10.45s
material-ui - node (v14.15.1, x64)
Memory used 445,505k (± 0.01%) 445,530k (± 0.01%) +26k (+ 0.01%) 445,466k 445,687k
Parse Time 1.77s (± 0.42%) 1.78s (± 0.47%) +0.01s (+ 0.51%) 1.76s 1.79s
Bind Time 0.69s (± 0.86%) 0.69s (± 0.43%) -0.00s (- 0.29%) 0.69s 0.70s
Check Time 13.09s (± 0.71%) 13.16s (± 0.44%) +0.07s (+ 0.52%) 13.02s 13.32s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.56s (± 0.61%) 15.64s (± 0.38%) +0.08s (+ 0.49%) 15.49s 15.81s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory9 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 45836 10
Baseline main 10

Developer Information:

Download Benchmark

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as ready for review October 18, 2021 01:23
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #45329. If you can get it accepted, this PR will have a better chance of being reviewed.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Experiment: narrow falsy strings to '' and falsy numbers to 0 Narrow falsy strings to '' and falsy numbers to 0 Oct 18, 2021
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

Code-wise this looks mostly fine to me, but @ahejlsberg and @RyanCavanaugh seemed to want to investigate the change more in the original issue and have yet to chime in? The last test results at least seem fine.

else if (type.flags & TypeFlags.Boolean) {
return falseType;
}
else if (type.flags & TypeFlags.String) {
Copy link
Member

Choose a reason for hiding this comment

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

There probably needs to be a TemplateString and StringMapping case now, too, since both type kinds could possibly contain the empty string.

@JoshuaKGoldberg
Copy link
Contributor Author

👍 I'll switch to draft pending #45329 (comment)

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft March 5, 2022 17:05
@JoshuaKGoldberg JoshuaKGoldberg deleted the experiment-falsy-narrowing-literals branch March 22, 2022 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

String union types narrowed to falsy should narrow string to ""
5 participants