Skip to content

In typeof x === object, type parameters extending unknown narrow like unknown itself - #49091

Closed
Wesley Wigham (weswigham) wants to merge 2 commits into
microsoft:mainfrom
weswigham:narrow-type-parameters-as-concrete-types
Closed

In typeof x === object, type parameters extending unknown narrow like unknown itself#49091
Wesley Wigham (weswigham) wants to merge 2 commits into
microsoft:mainfrom
weswigham:narrow-type-parameters-as-concrete-types

Conversation

@weswigham

Copy link
Copy Markdown
Member

In addition, improves relating keyof T and keyof NonNullable<T>-like types (to allow the relation) so that the impacts of, eg, chaining a typeof x === "object" and a truthiness check still produce compatible types.

Should fix#48468 even after the {} and unconstrained type parameter change is back in.

@weswigham

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Heya Wesley Wigham (@weswigham), I've started to run the extended test suite on this PR at dc319e7. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Heya Wesley Wigham (@weswigham), I've started to run the parallelized Definitely Typed test suite on this PR at dc319e7. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Heya Wesley Wigham (@weswigham), I've started to run the perf test suite on this PR at dc319e7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Heya Wesley Wigham (@weswigham), I've started to run the tarball bundle task on this PR at dc319e7. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Heya Wesley Wigham (@weswigham), I've started to run the diff-based community code test suite on this PR at dc319e7. You can monitor the build here.

Update: The results are in!

// then it doesn't affect the `keyof` query, and we can unwrap the conditional and relate the unwrapped source and target.
// There may be multiple stacked conditionals, such as `T extends null ? never : T extends undefined ? never : T : T`, so
// we need to repeat the unwrapping process.
while (isConditionalFilteringKeylessTypes(sourceType)) {

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

These keyof relationship improvements aren't strictly speaking tied to the typeof x === "object" narrowing change, but they do improve common uses of it (eg, narrowing within a loop and doing an index operation), so I think make sense to pair with it.

// no flags for all other types (including non-falsy literal types).
function getFalsyFlags(type: Type): TypeFlags {
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) :
type.flags & TypeFlags.Intersection ? reduceLeft(getApparentIntersectionTypes(type as IntersectionType), (memo, type) => memo | getFalsyFlags(type), 0 as TypeFacts) :

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Small change to falsy flag calculation to pick up T & undefined as falsy when it doesn't reduce to never.

@typescript-bot

Copy link
Copy Markdown
Contributor

Wesley Wigham (@weswigham)
Great news! no new errors were found between main..refs/pull/49091/merge

@typescript-bot

TypeScript Bot (typescript-bot) commented May 12, 2022

Copy link
Copy Markdown
Contributor

Hey Wesley Wigham (@weswigham), 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/126193/artifacts?artifactName=tgz&fileId=56B88CC3A017339360036F62DC6F70066B842AF856834661D3FDD044A3A7160F02&fileName=/typescript-4.8.0-insiders.20220512.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/pr-build@4.8.0-pr-49091-7".;

@typescript-bot

Copy link
Copy Markdown
Contributor

Wesley Wigham (@weswigham)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..49091

Metricmain49091DeltaBestWorst
Angular - node (v10.16.3, x64)
Memory used357,643k (± 0.02%)357,650k (± 0.03%)+8k (+ 0.00%)357,493k357,934k
Parse Time2.07s (± 0.66%)2.08s (± 0.49%)+0.00s (+ 0.05%)2.05s2.10s
Bind Time0.86s (± 0.72%)0.86s (± 0.88%)+0.00s (+ 0.35%)0.85s0.88s
Check Time5.82s (± 0.45%)5.87s (± 0.57%)+0.05s (+ 0.82%)5.81s5.96s
Emit Time6.00s (± 0.68%)6.01s (± 0.46%)+0.01s (+ 0.13%)5.94s6.07s
Total Time14.76s (± 0.34%)14.82s (± 0.41%)+0.06s (+ 0.39%)14.75s14.99s
Compiler-Unions - node (v10.16.3, x64)
Memory used205,870k (± 0.04%)205,986k (± 0.05%)+116k (+ 0.06%)205,759k206,295k
Parse Time0.83s (± 0.84%)0.84s (± 1.01%)+0.01s (+ 0.96%)0.81s0.85s
Bind Time0.52s (± 1.69%)0.51s (± 1.09%)-0.01s (- 2.10%)0.50s0.53s
Check Time7.92s (± 0.37%)7.93s (± 0.46%)+0.01s (+ 0.14%)7.82s8.02s
Emit Time2.50s (± 1.23%)2.51s (± 1.15%)+0.01s (+ 0.48%)2.45s2.58s
Total Time11.76s (± 0.42%)11.78s (± 0.41%)+0.02s (+ 0.16%)11.62s11.85s
Monaco - node (v10.16.3, x64)
Memory used343,766k (± 0.01%)343,793k (± 0.02%)+27k (+ 0.01%)343,593k343,900k
Parse Time1.58s (± 0.38%)1.59s (± 0.69%)+0.01s (+ 0.38%)1.57s1.62s
Bind Time0.75s (± 0.40%)0.75s (± 0.45%)+0.00s (+ 0.13%)0.75s0.76s
Check Time5.79s (± 0.44%)5.81s (± 0.40%)+0.01s (+ 0.22%)5.76s5.86s
Emit Time3.23s (± 0.64%)3.23s (± 0.60%)-0.00s (- 0.15%)3.19s3.26s
Total Time11.36s (± 0.24%)11.38s (± 0.28%)+0.02s (+ 0.18%)11.32s11.45s
TFS - node (v10.16.3, x64)
Memory used305,453k (± 0.02%)305,445k (± 0.03%)-8k (- 0.00%)305,213k305,652k
Parse Time1.29s (± 0.52%)1.29s (± 1.16%)+0.00s (+ 0.31%)1.27s1.34s
Bind Time0.71s (± 0.78%)0.71s (± 0.70%)+0.00s (+ 0.42%)0.71s0.73s
Check Time5.27s (± 0.70%)5.28s (± 0.37%)+0.02s (+ 0.34%)5.23s5.31s
Emit Time3.41s (± 1.09%)3.45s (± 0.85%)+0.03s (+ 0.97%)3.37s3.50s
Total Time10.68s (± 0.60%)10.73s (± 0.26%)+0.05s (+ 0.49%)10.68s10.82s
material-ui - node (v10.16.3, x64)
Memory used468,526k (± 0.01%)468,496k (± 0.01%)-31k (- 0.01%)468,370k468,638k
Parse Time1.82s (± 0.33%)1.83s (± 0.47%)+0.00s (+ 0.22%)1.81s1.85s
Bind Time0.68s (± 0.70%)0.67s (± 1.49%)-0.01s (- 1.03%)0.65s0.69s
Check Time14.32s (± 0.59%)14.32s (± 0.43%)-0.01s (- 0.05%)14.24s14.49s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time16.82s (± 0.49%)16.82s (± 0.40%)-0.01s (- 0.03%)16.74s17.03s
xstate - node (v10.16.3, x64)
Memory used571,851k (± 0.01%)575,274k (± 1.32%)+3,423k (+ 0.60%)571,587k605,983k
Parse Time2.58s (± 0.26%)2.60s (± 0.36%)+0.01s (+ 0.46%)2.58s2.62s
Bind Time1.00s (± 0.65%)1.01s (± 0.89%)+0.00s (+ 0.40%)0.99s1.03s
Check Time1.52s (± 0.51%)1.52s (± 0.24%)+0.00s (+ 0.26%)1.52s1.53s
Emit Time0.07s (± 3.14%)0.07s (± 3.14%)0.00s ( 0.00%)0.07s0.08s
Total Time5.18s (± 0.16%)5.20s (± 0.25%)+0.01s (+ 0.27%)5.17s5.22s
Angular - node (v12.1.0, x64)
Memory used335,355k (± 0.02%)335,365k (± 0.02%)+10k (+ 0.00%)335,181k335,479k
Parse Time2.07s (± 0.74%)2.08s (± 0.41%)+0.01s (+ 0.63%)2.06s2.10s
Bind Time0.82s (± 0.44%)0.82s (± 0.44%)0.00s ( 0.00%)0.82s0.83s
Check Time5.61s (± 0.32%)5.66s (± 0.63%)+0.04s (+ 0.77%)5.59s5.73s
Emit Time6.26s (± 0.71%)6.26s (± 0.89%)-0.00s (- 0.02%)6.15s6.39s
Total Time14.76s (± 0.31%)14.81s (± 0.54%)+0.05s (+ 0.36%)14.69s15.02s
Compiler-Unions - node (v12.1.0, x64)
Memory used193,502k (± 0.11%)193,443k (± 0.16%)-59k (- 0.03%)192,316k193,776k
Parse Time0.83s (± 1.26%)0.83s (± 1.04%)-0.00s (- 0.12%)0.82s0.86s
Bind Time0.53s (± 0.65%)0.53s (± 0.89%)+0.00s (+ 0.57%)0.52s0.54s
Check Time7.39s (± 0.31%)7.49s (± 0.50%)+0.11s (+ 1.45%)7.39s7.56s
Emit Time2.53s (± 1.24%)2.54s (± 0.81%)+0.01s (+ 0.36%)2.48s2.59s
Total Time11.27s (± 0.39%)11.39s (± 0.25%)+0.12s (+ 1.06%)11.31s11.44s
Monaco - node (v12.1.0, x64)
Memory used326,854k (± 0.02%)326,873k (± 0.02%)+19k (+ 0.01%)326,711k327,033k
Parse Time1.54s (± 0.96%)1.56s (± 0.44%)+0.01s (+ 0.91%)1.54s1.57s
Bind Time0.75s (± 1.27%)0.74s (± 0.45%)-0.01s (- 0.80%)0.73s0.75s
Check Time5.66s (± 0.54%)5.66s (± 0.54%)-0.00s (- 0.02%)5.60s5.74s
Emit Time3.27s (± 1.06%)3.26s (± 0.55%)-0.01s (- 0.40%)3.22s3.29s
Total Time11.22s (± 0.48%)11.21s (± 0.36%)-0.01s (- 0.08%)11.11s11.29s
TFS - node (v12.1.0, x64)
Memory used290,149k (± 0.02%)290,143k (± 0.02%)-6k (- 0.00%)290,057k290,262k
Parse Time1.30s (± 0.78%)1.30s (± 0.78%)0.00s ( 0.00%)1.27s1.31s
Bind Time0.72s (± 0.90%)0.73s (± 0.71%)+0.01s (+ 1.25%)0.72s0.74s
Check Time5.22s (± 0.35%)5.22s (± 0.70%)-0.00s (- 0.06%)5.11s5.30s
Emit Time3.50s (± 0.99%)3.50s (± 0.84%)-0.00s (- 0.09%)3.42s3.57s
Total Time10.74s (± 0.41%)10.74s (± 0.53%)+0.00s (+ 0.04%)10.63s10.86s
material-ui - node (v12.1.0, x64)
Memory used447,265k (± 0.09%)447,399k (± 0.08%)+134k (+ 0.03%)446,045k447,684k
Parse Time1.82s (± 0.55%)1.84s (± 0.67%)+0.02s (+ 0.93%)1.82s1.87s
Bind Time0.65s (± 0.61%)0.65s (± 0.72%)-0.00s (- 0.15%)0.64s0.66s
Check Time12.93s (± 0.69%)12.98s (± 0.74%)+0.05s (+ 0.42%)12.81s13.18s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.40s (± 0.57%)15.48s (± 0.62%)+0.07s (+ 0.47%)15.31s15.71s
xstate - node (v12.1.0, x64)
Memory used537,471k (± 0.02%)537,487k (± 0.01%)+16k (+ 0.00%)537,320k537,691k
Parse Time2.54s (± 0.35%)2.54s (± 0.63%)+0.00s (+ 0.20%)2.51s2.59s
Bind Time1.02s (± 0.87%)1.02s (± 0.66%)+0.01s (+ 0.69%)1.01s1.04s
Check Time1.47s (± 0.45%)1.47s (± 0.40%)+0.00s (+ 0.27%)1.46s1.49s
Emit Time0.07s (± 0.00%)0.07s (± 0.00%)0.00s ( 0.00%)0.07s0.07s
Total Time5.09s (± 0.31%)5.11s (± 0.36%)+0.02s (+ 0.35%)5.07s5.15s
Angular - node (v14.15.1, x64)
Memory used333,606k (± 0.01%)333,617k (± 0.01%)+11k (+ 0.00%)333,577k333,675k
Parse Time2.03s (± 0.18%)2.04s (± 0.51%)+0.01s (+ 0.39%)2.03s2.08s
Bind Time0.87s (± 0.34%)0.87s (± 0.94%)+0.01s (+ 0.58%)0.86s0.90s
Check Time5.62s (± 0.26%)5.64s (± 0.65%)+0.02s (+ 0.30%)5.55s5.72s
Emit Time6.30s (± 0.82%)6.31s (± 0.73%)+0.01s (+ 0.13%)6.22s6.41s
Total Time14.82s (± 0.34%)14.86s (± 0.47%)+0.04s (+ 0.30%)14.71s15.02s
Compiler-Unions - node (v14.15.1, x64)
Memory used192,285k (± 0.01%)192,272k (± 0.02%)-13k (- 0.01%)192,175k192,343k
Parse Time0.85s (± 1.03%)0.85s (± 0.73%)-0.00s (- 0.47%)0.84s0.86s
Bind Time0.56s (± 0.61%)0.56s (± 0.72%)+0.00s (+ 0.36%)0.55s0.57s
Check Time7.51s (± 0.39%)7.58s (± 0.33%)+0.07s (+ 0.87%)7.53s7.63s
Emit Time2.49s (± 0.64%)2.51s (± 0.44%)+0.01s (+ 0.56%)2.49s2.54s
Total Time11.42s (± 0.31%)11.49s (± 0.25%)+0.07s (+ 0.66%)11.42s11.55s
Monaco - node (v14.15.1, x64)
Memory used325,617k (± 0.00%)325,605k (± 0.01%)-12k (- 0.00%)325,559k325,660k
Parse Time1.57s (± 0.51%)1.57s (± 0.47%)+0.01s (+ 0.32%)1.56s1.59s
Bind Time0.77s (± 0.47%)0.77s (± 0.72%)-0.00s (- 0.26%)0.76s0.79s
Check Time5.53s (± 0.42%)5.54s (± 0.44%)+0.01s (+ 0.13%)5.50s5.62s
Emit Time3.33s (± 0.86%)3.35s (± 0.50%)+0.02s (+ 0.45%)3.29s3.37s
Total Time11.20s (± 0.42%)11.23s (± 0.28%)+0.03s (+ 0.25%)11.16s11.33s
TFS - node (v14.15.1, x64)
Memory used289,115k (± 0.01%)289,126k (± 0.01%)+11k (+ 0.00%)289,051k289,167k
Parse Time1.36s (± 0.62%)1.35s (± 1.13%)-0.00s (- 0.29%)1.31s1.39s
Bind Time0.72s (± 0.46%)0.72s (± 0.94%)+0.01s (+ 0.69%)0.71s0.74s
Check Time5.18s (± 0.40%)5.20s (± 0.52%)+0.02s (+ 0.44%)5.15s5.27s
Emit Time3.52s (± 1.70%)3.55s (± 1.87%)+0.03s (+ 0.97%)3.41s3.64s
Total Time10.78s (± 0.62%)10.84s (± 0.75%)+0.05s (+ 0.51%)10.66s10.99s
material-ui - node (v14.15.1, x64)
Memory used445,740k (± 0.00%)445,639k (± 0.06%)-101k (- 0.02%)444,535k445,792k
Parse Time1.87s (± 0.87%)1.88s (± 0.73%)+0.01s (+ 0.27%)1.85s1.92s
Bind Time0.69s (± 0.49%)0.70s (± 1.00%)+0.01s (+ 0.87%)0.69s0.72s
Check Time13.03s (± 0.46%)13.06s (± 0.71%)+0.03s (+ 0.19%)12.89s13.36s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.60s (± 0.43%)15.63s (± 0.64%)+0.04s (+ 0.22%)15.46s15.96s
xstate - node (v14.15.1, x64)
Memory used535,219k (± 0.00%)535,268k (± 0.00%)+49k (+ 0.01%)535,224k535,308k
Parse Time2.59s (± 0.53%)2.60s (± 0.89%)+0.01s (+ 0.43%)2.57s2.68s
Bind Time1.14s (± 0.66%)1.14s (± 0.35%)-0.00s (- 0.26%)1.13s1.15s
Check Time1.52s (± 0.32%)1.52s (± 0.54%)+0.01s (+ 0.59%)1.51s1.54s
Emit Time0.07s (± 0.00%)0.07s (± 0.00%)0.00s ( 0.00%)0.07s0.07s
Total Time5.32s (± 0.28%)5.34s (± 0.53%)+0.02s (+ 0.38%)5.30s5.43s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory2 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)
  • xstate - node (v10.16.3, x64)
  • xstate - node (v12.1.0, x64)
  • xstate - node (v14.15.1, x64)
BenchmarkNameIterations
Current4909110
Baselinemain10

Developer Information:

Download Benchmark

}

// #48468 but with an explicit constraint so as to not trigger the `{}` and unconstrained type parameter bug
function deepEquals<T extends unknown>(a: T, b: T) {

Choose a reason for hiding this comment

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

Can you have a test for both the explicit and non-explicit constraint?

FWIW I have tests at #48576.

@weswigham

Copy link
Copy Markdown
MemberAuthor

DT looks clean (some unrelated intl change is making it fail but otherwise clean), RWC has removed an error on an Object.keys call from the better narrowing. Looks good!

@weswigham

Copy link
Copy Markdown
MemberAuthor

This mostly got folded into #49119. I'll extract the improvements to keyof relations into a new PR, since those changes are the only thing that didn't and are pretty standalone.

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

Archived in project

Development

Successfully merging this pull request may close these issues.

Generic type no longer narrowed as expected without extends object

4 participants

@weswigham@typescript-bot@DanielRosenwasser@sandersn