Skip to content

Widen boolean literals when contextual type is full boolean type - #48368

Closed
Anders Hejlsberg (ahejlsberg) wants to merge 5 commits into
mainfrom
fix48363
Closed

Widen boolean literals when contextual type is full boolean type#48368
Anders Hejlsberg (ahejlsberg) wants to merge 5 commits into
mainfrom
fix48363

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

Fixes#48363.

Also fixes issue mentioned here because boolean literals contextually typed by boolean are now widened.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the extended test suite on this PR at b54de54. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

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

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized Definitely Typed test suite on this PR at b54de54. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the diff-based community code test suite on this PR at b54de54. You can monitor the build here.

Update: The results are in!

@typescript-bot

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..48368

Metricmain48368DeltaBestWorst
Angular - node (v14.15.1, x64)
Memory used332,421k (± 0.00%)332,455k (± 0.00%)+34k (+ 0.01%)332,422k332,499k
Parse Time2.01s (± 0.38%)2.04s (± 0.36%)+0.02s (+ 1.14%)2.02s2.06s
Bind Time0.86s (± 0.43%)0.87s (± 0.51%)+0.00s (+ 0.35%)0.86s0.88s
Check Time5.58s (± 0.55%)5.60s (± 0.45%)+0.02s (+ 0.45%)5.53s5.65s
Emit Time6.28s (± 0.71%)6.32s (± 0.80%)+0.04s (+ 0.59%)6.21s6.45s
Total Time14.74s (± 0.42%)14.82s (± 0.47%)+0.09s (+ 0.58%)14.70s15.01s
Compiler-Unions - node (v14.15.1, x64)
Memory used192,805k (± 0.59%)193,835k (± 0.60%)+1,030k (+ 0.53%)191,837k195,140k
Parse Time0.85s (± 0.40%)0.85s (± 0.56%)0.00s ( 0.00%)0.84s0.86s
Bind Time0.56s (± 0.71%)0.56s (± 0.93%)0.00s ( 0.00%)0.55s0.57s
Check Time7.48s (± 0.74%)7.43s (± 0.37%)-0.05s (- 0.70%)7.36s7.48s
Emit Time2.51s (± 1.50%)2.48s (± 0.54%)-0.03s (- 1.35%)2.45s2.51s
Total Time11.41s (± 0.65%)11.32s (± 0.28%)-0.09s (- 0.78%)11.24s11.38s
Monaco - node (v14.15.1, x64)
Memory used325,401k (± 0.01%)325,363k (± 0.00%)-38k (- 0.01%)325,340k325,393k
Parse Time1.58s (± 0.62%)1.58s (± 0.61%)-0.00s (- 0.19%)1.55s1.59s
Bind Time0.78s (± 0.85%)0.77s (± 0.47%)-0.01s (- 0.77%)0.77s0.78s
Check Time5.49s (± 0.45%)5.46s (± 0.40%)-0.03s (- 0.47%)5.41s5.51s
Emit Time3.33s (± 0.78%)3.29s (± 0.55%)-0.04s (- 1.11%)3.26s3.34s
Total Time11.17s (± 0.37%)11.10s (± 0.33%)-0.07s (- 0.64%)11.01s11.18s
TFS - node (v14.15.1, x64)
Memory used288,859k (± 0.01%)288,857k (± 0.01%)-2k (- 0.00%)288,801k288,904k
Parse Time1.34s (± 1.22%)1.32s (± 0.96%)-0.02s (- 1.64%)1.29s1.35s
Bind Time0.73s (± 1.04%)0.73s (± 0.71%)-0.00s (- 0.54%)0.72s0.74s
Check Time5.11s (± 0.55%)5.11s (± 0.35%)+0.00s (+ 0.04%)5.08s5.16s
Emit Time3.51s (± 2.21%)3.52s (± 1.89%)+0.01s (+ 0.26%)3.38s3.63s
Total Time10.69s (± 0.86%)10.68s (± 0.77%)-0.02s (- 0.15%)10.49s10.83s
material-ui - node (v14.15.1, x64)
Memory used453,485k (± 0.08%)453,590k (± 0.07%)+104k (+ 0.02%)452,376k453,798k
Parse Time1.86s (± 0.54%)1.85s (± 0.33%)-0.02s (- 0.86%)1.84s1.86s
Bind Time0.71s (± 0.97%)0.70s (± 1.18%)-0.00s (- 0.00%)0.69s0.72s
Check Time20.97s (± 0.59%)20.49s (± 0.84%)-0.48s (- 2.28%)20.25s20.93s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time23.55s (± 0.55%)23.05s (± 0.76%)-0.50s (- 2.11%)22.78s23.48s
xstate - node (v14.15.1, x64)
Memory used535,220k (± 0.00%)535,233k (± 0.01%)+13k (+ 0.00%)535,184k535,298k
Parse Time2.58s (± 0.61%)2.57s (± 0.43%)-0.01s (- 0.35%)2.55s2.59s
Bind Time1.15s (± 1.24%)1.13s (± 0.77%)-0.02s (- 1.48%)1.12s1.15s
Check Time1.51s (± 0.65%)1.51s (± 0.73%)+0.00s (+ 0.13%)1.49s1.54s
Emit Time0.07s (± 4.13%)0.07s (± 0.00%)-0.00s (- 2.78%)0.07s0.07s
Total Time5.31s (± 0.27%)5.29s (± 0.25%)-0.03s (- 0.49%)5.26s5.32s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory4 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)
  • xstate - node (v14.15.1, x64)
BenchmarkNameIterations
Current4836810
Baselinemain10

Developer Information:

Download Benchmark

@typescript-bot

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg)
Great news! no new errors were found between main..refs/pull/48368/merge

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

Apart from lots of false and true types changing to boolean (as expected), there are really only two notable changes in the baselines. One is in excessPropertyCheckWithMultipleDiscriminants.ts, simplified to:

letx: {kind: false,x: number}|{kind: boolean,x: string}={kind: false,x: 42};

Previously this would match against the first variant and succeed. It now matches against the second variant (because the kind: false in the object literal is given type boolean as it is contextually typed by boolean). It's possible to get the old behavior by writing kind: false as const. I think this change is acceptable.

The other change is in inferFromGenericFunctionReturnTypes3.ts:

declarefunctionfoldLeft<U>(z: U,f: (acc: U,t: boolean)=>U): U;letres: boolean=foldLeft(true,(acc,t)=>acc&&t);// Was error, now ok

Previously the true literal wasn't widened and therefore acc and t would have type true, which doesn't work out. We now widen and the example works as expected.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 22, 2022

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized Definitely Typed test suite on this PR at af5bd9e. You can monitor the build here.

@fatcerberus

Copy link
Copy Markdown

Someone brought this up in Discord:

letfoo: boolean=false;

With the change in this PR, does this no longer narrow-on-assignment to false?

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Mar 23, 2022

Copy link
Copy Markdown
MemberAuthor

Several Definitely Typed packages are adversely affected. Closing this PR and putting up #48380 with less impact.

@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent boolean literal types

3 participants

@ahejlsberg@typescript-bot@fatcerberus