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

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

Conversation

weswigham
Copy link
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
Member Author

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 12, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 12, 2022

Heya @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
Copy link
Collaborator

typescript-bot commented May 12, 2022

Heya @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
Copy link
Collaborator

typescript-bot commented May 12, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 12, 2022

Heya @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
Member Author

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.

// Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
// 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
Member Author

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
Collaborator

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 12, 2022

Hey @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
Collaborator

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

Here they are:

Comparison Report - main..49091

Metric main 49091 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 357,643k (± 0.02%) 357,650k (± 0.03%) +8k (+ 0.00%) 357,493k 357,934k
Parse Time 2.07s (± 0.66%) 2.08s (± 0.49%) +0.00s (+ 0.05%) 2.05s 2.10s
Bind Time 0.86s (± 0.72%) 0.86s (± 0.88%) +0.00s (+ 0.35%) 0.85s 0.88s
Check Time 5.82s (± 0.45%) 5.87s (± 0.57%) +0.05s (+ 0.82%) 5.81s 5.96s
Emit Time 6.00s (± 0.68%) 6.01s (± 0.46%) +0.01s (+ 0.13%) 5.94s 6.07s
Total Time 14.76s (± 0.34%) 14.82s (± 0.41%) +0.06s (+ 0.39%) 14.75s 14.99s
Compiler-Unions - node (v10.16.3, x64)
Memory used 205,870k (± 0.04%) 205,986k (± 0.05%) +116k (+ 0.06%) 205,759k 206,295k
Parse Time 0.83s (± 0.84%) 0.84s (± 1.01%) +0.01s (+ 0.96%) 0.81s 0.85s
Bind Time 0.52s (± 1.69%) 0.51s (± 1.09%) -0.01s (- 2.10%) 0.50s 0.53s
Check Time 7.92s (± 0.37%) 7.93s (± 0.46%) +0.01s (+ 0.14%) 7.82s 8.02s
Emit Time 2.50s (± 1.23%) 2.51s (± 1.15%) +0.01s (+ 0.48%) 2.45s 2.58s
Total Time 11.76s (± 0.42%) 11.78s (± 0.41%) +0.02s (+ 0.16%) 11.62s 11.85s
Monaco - node (v10.16.3, x64)
Memory used 343,766k (± 0.01%) 343,793k (± 0.02%) +27k (+ 0.01%) 343,593k 343,900k
Parse Time 1.58s (± 0.38%) 1.59s (± 0.69%) +0.01s (+ 0.38%) 1.57s 1.62s
Bind Time 0.75s (± 0.40%) 0.75s (± 0.45%) +0.00s (+ 0.13%) 0.75s 0.76s
Check Time 5.79s (± 0.44%) 5.81s (± 0.40%) +0.01s (+ 0.22%) 5.76s 5.86s
Emit Time 3.23s (± 0.64%) 3.23s (± 0.60%) -0.00s (- 0.15%) 3.19s 3.26s
Total Time 11.36s (± 0.24%) 11.38s (± 0.28%) +0.02s (+ 0.18%) 11.32s 11.45s
TFS - node (v10.16.3, x64)
Memory used 305,453k (± 0.02%) 305,445k (± 0.03%) -8k (- 0.00%) 305,213k 305,652k
Parse Time 1.29s (± 0.52%) 1.29s (± 1.16%) +0.00s (+ 0.31%) 1.27s 1.34s
Bind Time 0.71s (± 0.78%) 0.71s (± 0.70%) +0.00s (+ 0.42%) 0.71s 0.73s
Check Time 5.27s (± 0.70%) 5.28s (± 0.37%) +0.02s (+ 0.34%) 5.23s 5.31s
Emit Time 3.41s (± 1.09%) 3.45s (± 0.85%) +0.03s (+ 0.97%) 3.37s 3.50s
Total Time 10.68s (± 0.60%) 10.73s (± 0.26%) +0.05s (+ 0.49%) 10.68s 10.82s
material-ui - node (v10.16.3, x64)
Memory used 468,526k (± 0.01%) 468,496k (± 0.01%) -31k (- 0.01%) 468,370k 468,638k
Parse Time 1.82s (± 0.33%) 1.83s (± 0.47%) +0.00s (+ 0.22%) 1.81s 1.85s
Bind Time 0.68s (± 0.70%) 0.67s (± 1.49%) -0.01s (- 1.03%) 0.65s 0.69s
Check Time 14.32s (± 0.59%) 14.32s (± 0.43%) -0.01s (- 0.05%) 14.24s 14.49s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.82s (± 0.49%) 16.82s (± 0.40%) -0.01s (- 0.03%) 16.74s 17.03s
xstate - node (v10.16.3, x64)
Memory used 571,851k (± 0.01%) 575,274k (± 1.32%) +3,423k (+ 0.60%) 571,587k 605,983k
Parse Time 2.58s (± 0.26%) 2.60s (± 0.36%) +0.01s (+ 0.46%) 2.58s 2.62s
Bind Time 1.00s (± 0.65%) 1.01s (± 0.89%) +0.00s (+ 0.40%) 0.99s 1.03s
Check Time 1.52s (± 0.51%) 1.52s (± 0.24%) +0.00s (+ 0.26%) 1.52s 1.53s
Emit Time 0.07s (± 3.14%) 0.07s (± 3.14%) 0.00s ( 0.00%) 0.07s 0.08s
Total Time 5.18s (± 0.16%) 5.20s (± 0.25%) +0.01s (+ 0.27%) 5.17s 5.22s
Angular - node (v12.1.0, x64)
Memory used 335,355k (± 0.02%) 335,365k (± 0.02%) +10k (+ 0.00%) 335,181k 335,479k
Parse Time 2.07s (± 0.74%) 2.08s (± 0.41%) +0.01s (+ 0.63%) 2.06s 2.10s
Bind Time 0.82s (± 0.44%) 0.82s (± 0.44%) 0.00s ( 0.00%) 0.82s 0.83s
Check Time 5.61s (± 0.32%) 5.66s (± 0.63%) +0.04s (+ 0.77%) 5.59s 5.73s
Emit Time 6.26s (± 0.71%) 6.26s (± 0.89%) -0.00s (- 0.02%) 6.15s 6.39s
Total Time 14.76s (± 0.31%) 14.81s (± 0.54%) +0.05s (+ 0.36%) 14.69s 15.02s
Compiler-Unions - node (v12.1.0, x64)
Memory used 193,502k (± 0.11%) 193,443k (± 0.16%) -59k (- 0.03%) 192,316k 193,776k
Parse Time 0.83s (± 1.26%) 0.83s (± 1.04%) -0.00s (- 0.12%) 0.82s 0.86s
Bind Time 0.53s (± 0.65%) 0.53s (± 0.89%) +0.00s (+ 0.57%) 0.52s 0.54s
Check Time 7.39s (± 0.31%) 7.49s (± 0.50%) +0.11s (+ 1.45%) 7.39s 7.56s
Emit Time 2.53s (± 1.24%) 2.54s (± 0.81%) +0.01s (+ 0.36%) 2.48s 2.59s
Total Time 11.27s (± 0.39%) 11.39s (± 0.25%) +0.12s (+ 1.06%) 11.31s 11.44s
Monaco - node (v12.1.0, x64)
Memory used 326,854k (± 0.02%) 326,873k (± 0.02%) +19k (+ 0.01%) 326,711k 327,033k
Parse Time 1.54s (± 0.96%) 1.56s (± 0.44%) +0.01s (+ 0.91%) 1.54s 1.57s
Bind Time 0.75s (± 1.27%) 0.74s (± 0.45%) -0.01s (- 0.80%) 0.73s 0.75s
Check Time 5.66s (± 0.54%) 5.66s (± 0.54%) -0.00s (- 0.02%) 5.60s 5.74s
Emit Time 3.27s (± 1.06%) 3.26s (± 0.55%) -0.01s (- 0.40%) 3.22s 3.29s
Total Time 11.22s (± 0.48%) 11.21s (± 0.36%) -0.01s (- 0.08%) 11.11s 11.29s
TFS - node (v12.1.0, x64)
Memory used 290,149k (± 0.02%) 290,143k (± 0.02%) -6k (- 0.00%) 290,057k 290,262k
Parse Time 1.30s (± 0.78%) 1.30s (± 0.78%) 0.00s ( 0.00%) 1.27s 1.31s
Bind Time 0.72s (± 0.90%) 0.73s (± 0.71%) +0.01s (+ 1.25%) 0.72s 0.74s
Check Time 5.22s (± 0.35%) 5.22s (± 0.70%) -0.00s (- 0.06%) 5.11s 5.30s
Emit Time 3.50s (± 0.99%) 3.50s (± 0.84%) -0.00s (- 0.09%) 3.42s 3.57s
Total Time 10.74s (± 0.41%) 10.74s (± 0.53%) +0.00s (+ 0.04%) 10.63s 10.86s
material-ui - node (v12.1.0, x64)
Memory used 447,265k (± 0.09%) 447,399k (± 0.08%) +134k (+ 0.03%) 446,045k 447,684k
Parse Time 1.82s (± 0.55%) 1.84s (± 0.67%) +0.02s (+ 0.93%) 1.82s 1.87s
Bind Time 0.65s (± 0.61%) 0.65s (± 0.72%) -0.00s (- 0.15%) 0.64s 0.66s
Check Time 12.93s (± 0.69%) 12.98s (± 0.74%) +0.05s (+ 0.42%) 12.81s 13.18s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.40s (± 0.57%) 15.48s (± 0.62%) +0.07s (+ 0.47%) 15.31s 15.71s
xstate - node (v12.1.0, x64)
Memory used 537,471k (± 0.02%) 537,487k (± 0.01%) +16k (+ 0.00%) 537,320k 537,691k
Parse Time 2.54s (± 0.35%) 2.54s (± 0.63%) +0.00s (+ 0.20%) 2.51s 2.59s
Bind Time 1.02s (± 0.87%) 1.02s (± 0.66%) +0.01s (+ 0.69%) 1.01s 1.04s
Check Time 1.47s (± 0.45%) 1.47s (± 0.40%) +0.00s (+ 0.27%) 1.46s 1.49s
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) 0.00s ( 0.00%) 0.07s 0.07s
Total Time 5.09s (± 0.31%) 5.11s (± 0.36%) +0.02s (+ 0.35%) 5.07s 5.15s
Angular - node (v14.15.1, x64)
Memory used 333,606k (± 0.01%) 333,617k (± 0.01%) +11k (+ 0.00%) 333,577k 333,675k
Parse Time 2.03s (± 0.18%) 2.04s (± 0.51%) +0.01s (+ 0.39%) 2.03s 2.08s
Bind Time 0.87s (± 0.34%) 0.87s (± 0.94%) +0.01s (+ 0.58%) 0.86s 0.90s
Check Time 5.62s (± 0.26%) 5.64s (± 0.65%) +0.02s (+ 0.30%) 5.55s 5.72s
Emit Time 6.30s (± 0.82%) 6.31s (± 0.73%) +0.01s (+ 0.13%) 6.22s 6.41s
Total Time 14.82s (± 0.34%) 14.86s (± 0.47%) +0.04s (+ 0.30%) 14.71s 15.02s
Compiler-Unions - node (v14.15.1, x64)
Memory used 192,285k (± 0.01%) 192,272k (± 0.02%) -13k (- 0.01%) 192,175k 192,343k
Parse Time 0.85s (± 1.03%) 0.85s (± 0.73%) -0.00s (- 0.47%) 0.84s 0.86s
Bind Time 0.56s (± 0.61%) 0.56s (± 0.72%) +0.00s (+ 0.36%) 0.55s 0.57s
Check Time 7.51s (± 0.39%) 7.58s (± 0.33%) +0.07s (+ 0.87%) 7.53s 7.63s
Emit Time 2.49s (± 0.64%) 2.51s (± 0.44%) +0.01s (+ 0.56%) 2.49s 2.54s
Total Time 11.42s (± 0.31%) 11.49s (± 0.25%) +0.07s (+ 0.66%) 11.42s 11.55s
Monaco - node (v14.15.1, x64)
Memory used 325,617k (± 0.00%) 325,605k (± 0.01%) -12k (- 0.00%) 325,559k 325,660k
Parse Time 1.57s (± 0.51%) 1.57s (± 0.47%) +0.01s (+ 0.32%) 1.56s 1.59s
Bind Time 0.77s (± 0.47%) 0.77s (± 0.72%) -0.00s (- 0.26%) 0.76s 0.79s
Check Time 5.53s (± 0.42%) 5.54s (± 0.44%) +0.01s (+ 0.13%) 5.50s 5.62s
Emit Time 3.33s (± 0.86%) 3.35s (± 0.50%) +0.02s (+ 0.45%) 3.29s 3.37s
Total Time 11.20s (± 0.42%) 11.23s (± 0.28%) +0.03s (+ 0.25%) 11.16s 11.33s
TFS - node (v14.15.1, x64)
Memory used 289,115k (± 0.01%) 289,126k (± 0.01%) +11k (+ 0.00%) 289,051k 289,167k
Parse Time 1.36s (± 0.62%) 1.35s (± 1.13%) -0.00s (- 0.29%) 1.31s 1.39s
Bind Time 0.72s (± 0.46%) 0.72s (± 0.94%) +0.01s (+ 0.69%) 0.71s 0.74s
Check Time 5.18s (± 0.40%) 5.20s (± 0.52%) +0.02s (+ 0.44%) 5.15s 5.27s
Emit Time 3.52s (± 1.70%) 3.55s (± 1.87%) +0.03s (+ 0.97%) 3.41s 3.64s
Total Time 10.78s (± 0.62%) 10.84s (± 0.75%) +0.05s (+ 0.51%) 10.66s 10.99s
material-ui - node (v14.15.1, x64)
Memory used 445,740k (± 0.00%) 445,639k (± 0.06%) -101k (- 0.02%) 444,535k 445,792k
Parse Time 1.87s (± 0.87%) 1.88s (± 0.73%) +0.01s (+ 0.27%) 1.85s 1.92s
Bind Time 0.69s (± 0.49%) 0.70s (± 1.00%) +0.01s (+ 0.87%) 0.69s 0.72s
Check Time 13.03s (± 0.46%) 13.06s (± 0.71%) +0.03s (+ 0.19%) 12.89s 13.36s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.60s (± 0.43%) 15.63s (± 0.64%) +0.04s (+ 0.22%) 15.46s 15.96s
xstate - node (v14.15.1, x64)
Memory used 535,219k (± 0.00%) 535,268k (± 0.00%) +49k (+ 0.01%) 535,224k 535,308k
Parse Time 2.59s (± 0.53%) 2.60s (± 0.89%) +0.01s (+ 0.43%) 2.57s 2.68s
Bind Time 1.14s (± 0.66%) 1.14s (± 0.35%) -0.00s (- 0.26%) 1.13s 1.15s
Check Time 1.52s (± 0.32%) 1.52s (± 0.54%) +0.01s (+ 0.59%) 1.51s 1.54s
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) 0.00s ( 0.00%) 0.07s 0.07s
Total Time 5.32s (± 0.28%) 5.34s (± 0.53%) +0.02s (+ 0.38%) 5.30s 5.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)
Benchmark Name Iterations
Current 49091 10
Baseline main 10

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) {
Copy link
Member

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
Member Author

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
Member Author

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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team 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.

Generic type no longer narrowed as expected without extends object
3 participants