Skip to content

Reduce intersections of constrained type variables and primitive types - #56515

Merged
Anders Hejlsberg (ahejlsberg) merged 3 commits into
mainfrom
typeVariableIntersections
Nov 30, 2023
Merged

Reduce intersections of constrained type variables and primitive types#56515
Anders Hejlsberg (ahejlsberg) merged 3 commits into
mainfrom
typeVariableIntersections

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

With this PR we reduce intersections of constrained type variables and primitive types as follows:

  • An intersection T & P or P & T, where T's constraint is a subtype of P, is reduced to just T. For example, given T extends "a" | "b", the intersection T & string is reduced to just T.
  • An intersection T & P or P & T, where no constituent of T's constraint is a subtype of P, and P is not a subtype of T's constraint, is reduced to never. For example, given T extends "a" | "b", the intersection T & number is reduced to never.
  • In a union containing intersections of a type variable and primitive types that fully cover the constraint of that type variable, the intersections are replaced with just the type variable. For example, given T extends "a" | "b", the union type T & "a" | T & "b" is reduced to just T.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 22, 2023

Copy link
Copy Markdown
Contributor

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

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 22, 2023

Copy link
Copy Markdown
Contributor

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

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 22, 2023

Copy link
Copy Markdown
Contributor

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

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 22, 2023

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the diff-based top-repos suite on this PR at 5b6f969. You can monitor the build here.

Update: The results are in!


value; // T & {}
>value : T & ({} | null)
>value : never

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems funky; T could be 42 here but it's thinking that comparison is impossible and made never.

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.

It's actually existing behavior that is being revealed by the reduction of T & ({} | null) to just T. We have logic somewhere in the comparable relation that considers a naked type parameter constrained to {} to be something that always represents an object.

@typescript-bot

Copy link
Copy Markdown
Contributor

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

Here they are:

Compiler

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Angular - node (v18.15.0, x64)
Memory used295,193k (± 0.01%)295,206k (± 0.01%)~295,188k295,236kp=0.229 n=6
Parse Time2.64s (± 0.29%)2.65s (± 0.32%)~2.64s2.66sp=0.209 n=6
Bind Time0.83s (± 0.99%)0.82s (± 0.50%)~0.82s0.83sp=0.248 n=6
Check Time8.04s (± 0.21%)8.05s (± 0.31%)~8.00s8.07sp=0.329 n=6
Emit Time7.07s (± 0.39%)7.07s (± 0.42%)~7.04s7.11sp=0.743 n=6
Total Time18.57s (± 0.18%)18.59s (± 0.11%)~18.56s18.62sp=0.291 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used192,514k (± 1.21%)192,651k (± 1.58%)~190,638k196,700kp=0.936 n=6
Parse Time1.35s (± 1.05%)1.35s (± 1.09%)~1.34s1.38sp=1.000 n=6
Bind Time0.72s (± 0.00%)0.72s (± 0.00%)~0.72s0.72sp=1.000 n=6
Check Time9.21s (± 0.42%)9.16s (± 0.69%)~9.10s9.25sp=0.418 n=6
Emit Time2.61s (± 0.39%)2.61s (± 0.31%)~2.60s2.62sp=0.932 n=6
Total Time13.88s (± 0.24%)13.85s (± 0.46%)~13.77s13.93sp=0.470 n=6
Monaco - node (v18.15.0, x64)
Memory used347,373k (± 0.01%)347,372k (± 0.00%)~347,350k347,398kp=0.575 n=6
Parse Time2.46s (± 0.42%)2.46s (± 0.40%)~2.45s2.47sp=0.933 n=6
Bind Time0.93s (± 0.56%)0.92s (± 0.56%)~0.92s0.93sp=0.311 n=6
Check Time6.91s (± 0.56%)6.91s (± 0.51%)~6.87s6.96sp=1.000 n=6
Emit Time4.05s (± 0.19%)4.05s (± 0.20%)~4.04s4.06sp=0.729 n=6
Total Time14.35s (± 0.26%)14.35s (± 0.23%)~14.30s14.40sp=1.000 n=6
TFS - node (v18.15.0, x64)
Memory used302,633k (± 0.00%)302,638k (± 0.01%)~302,618k302,662kp=0.688 n=6
Parse Time1.99s (± 1.66%)2.00s (± 0.86%)~1.98s2.03sp=0.686 n=6
Bind Time1.00s (± 0.75%)1.01s (± 1.20%)~0.99s1.02sp=0.240 n=6
Check Time6.26s (± 0.46%)6.28s (± 0.41%)~6.25s6.32sp=0.124 n=6
Emit Time3.59s (± 0.48%)3.58s (± 0.42%)~3.56s3.60sp=0.279 n=6
Total Time12.84s (± 0.39%)12.87s (± 0.26%)~12.82s12.91sp=0.295 n=6
material-ui - node (v18.15.0, x64)
Memory used470,630k (± 0.02%)470,642k (± 0.01%)~470,600k470,681kp=0.378 n=6
Parse Time2.57s (± 0.63%)2.57s (± 0.79%)~2.55s2.60sp=0.867 n=6
Bind Time0.99s (± 1.22%)0.98s (± 0.56%)~0.98s0.99sp=0.235 n=6
Check Time16.74s (± 0.49%)16.76s (± 0.28%)~16.70s16.83sp=0.375 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time20.30s (± 0.50%)20.32s (± 0.27%)~20.25s20.41sp=0.372 n=6
xstate - node (v18.15.0, x64)
Memory used511,983k (± 0.01%)511,891k (± 0.01%)-91k (- 0.02%)511,862k511,941kp=0.008 n=6
Parse Time3.27s (± 0.23%)3.27s (± 0.36%)~3.25s3.28sp=0.796 n=6
Bind Time1.54s (± 0.00%)1.55s (± 0.26%)+0.01s (+ 0.54%)1.54s1.55sp=0.007 n=6
Check Time2.79s (± 0.67%)2.77s (± 0.95%)~2.74s2.80sp=0.252 n=6
Emit Time0.08s (± 0.00%)0.08s (± 4.99%)~0.08s0.09sp=0.405 n=6
Total Time7.68s (± 0.27%)7.67s (± 0.35%)~7.64s7.70sp=0.466 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) Here are the results of running the user test suite comparing main and refs/pull/56515/merge:

There were infrastructure failures potentially unrelated to your change:

  • 2 instances of "Package install failed"

Otherwise...

Something interesting changed - please have a look.

Details

puppeteer

packages/browsers/test/src/tsconfig.json

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Anders Hejlsberg (@ahejlsberg), the results of running the DT tests are ready.
There were interesting changes:

Branch only errors:

Package: enzyme
Error:

Error: /home/vsts/work/1/DefinitelyTyped/types/enzyme/enzyme-tests.tsx:106:9
ERROR: 106:9 expect TypeScript@local compile error: Unused '@ts-expect-error' directive.
at testTypesVersion (/home/vsts/work/1/DefinitelyTyped/node_modules/.pnpm/@definitelytyped+dtslint@0.0.196_typescript@5.4.0-dev.20231122/node_modules/@definitelytyped/dtslint/dist/index.js:176:15)
at async runTests (/home/vsts/work/1/DefinitelyTyped/node_modules/.pnpm/@definitelytyped+dtslint@0.0.196_typescript@5.4.0-dev.20231122/node_modules/@definitelytyped/dtslint/dist/index.js:135:9)

You can check the log here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) Here are the results of running the top-repos suite comparing main and refs/pull/56515/merge:

Everything looks good!

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

The new error in the Definitely Typed enzyme package is related to a type of the form

typeKeys<P>={[KinkeyofP]: K}[keyofP];

In the enzyme package there is an application of NonNullable<Keys<P>>, which corresponds to Keys<P> & {}. The constraint of Keys<P> is string | number | symbol (i.e. the type of any valid property key). With the new functionality in this PR, the intersection Keys<P> & {} gets reduced to just Keys<P> because the constraint indicates the type can't be null or undefined.

However, upon instantiation with a type that contains optional properties, the resulting type will actually include undefined. For example

typeFoo={a?: string;b: number;}typeFooKeys=Keys<Foo>;// "a" | "b" | undefined

Since Keys is homomorphic, modifiers (such as optionality) are copied from the properties of the source type, and that ends up adding undefined to the optional properties. This behavior is as expected.

The issue here really is that the constraint of Keys<P> should include undefined--because the type might be applied to something with optional properties. I tried implementing that, but it causes a whole slew of new errors that technically are correct, but break existing code. So, I think the cure is worse than the disease there.

Generally, types like Keys<P> should be written to exclude optionality from the properties:

typeKeys<P>={[KinkeyofP]-?: K}[keyofP];

With this change there are no new errors in enzyme.

@gabritto

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) pack this

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

Heya Gabriela Araujo Britto (@gabritto), I've started to run the tarball bundle task on this PR at 5b6f969. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

Hey Gabriela Araujo Britto (@gabritto), 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/158848/artifacts?artifactName=tgz&fileId=EB88B1ED0A330275E743D467B388586ABE3607D9A44B45B5C76D42D6978682EF02&fileName=/typescript-5.4.0-insiders.20231128.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@5.4.0-pr-56515-12".;

@weswighamWesley Wigham (weswigham) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I had to include a similar change in the not types PR to get them to work decently - I think this is a pretty good simplification to perform on union (and intersection) construction (since making the type smaller is basically always good).

The one thing I worry a bit about though, if when you reduce T & U to just T, you remove the intersection entirely, which means you remove the type an alias could have been pinned to, which could be an observable change. Hopefully it doesn't matter in practice - I'm hoping there's no type Int<T extends number> = T & number; style types too prevalent in the wild.

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.

5 participants

@ahejlsberg@typescript-bot@gabritto@weswigham@jakebailey