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

Reduce intersections of constrained type variables and primitive types #56515

Merged
merged 3 commits into from Nov 30, 2023

Conversation

ahejlsberg
Copy link
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.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 22, 2023
@ahejlsberg
Copy link
Member Author

@typescript-bot test top100
@typescript-bot user test this
@typescript-bot run dt
@typescript-bot perf test this faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 22, 2023

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

typescript-bot commented Nov 22, 2023

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

typescript-bot commented Nov 22, 2023

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

typescript-bot commented Nov 22, 2023

Heya @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!

>42 : 42

value; // T & {}
>value : T & ({} | null)
>value : never
Copy link
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
Member Author

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
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,193k (± 0.01%) 295,206k (± 0.01%) ~ 295,188k 295,236k p=0.229 n=6
Parse Time 2.64s (± 0.29%) 2.65s (± 0.32%) ~ 2.64s 2.66s p=0.209 n=6
Bind Time 0.83s (± 0.99%) 0.82s (± 0.50%) ~ 0.82s 0.83s p=0.248 n=6
Check Time 8.04s (± 0.21%) 8.05s (± 0.31%) ~ 8.00s 8.07s p=0.329 n=6
Emit Time 7.07s (± 0.39%) 7.07s (± 0.42%) ~ 7.04s 7.11s p=0.743 n=6
Total Time 18.57s (± 0.18%) 18.59s (± 0.11%) ~ 18.56s 18.62s p=0.291 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 192,514k (± 1.21%) 192,651k (± 1.58%) ~ 190,638k 196,700k p=0.936 n=6
Parse Time 1.35s (± 1.05%) 1.35s (± 1.09%) ~ 1.34s 1.38s p=1.000 n=6
Bind Time 0.72s (± 0.00%) 0.72s (± 0.00%) ~ 0.72s 0.72s p=1.000 n=6
Check Time 9.21s (± 0.42%) 9.16s (± 0.69%) ~ 9.10s 9.25s p=0.418 n=6
Emit Time 2.61s (± 0.39%) 2.61s (± 0.31%) ~ 2.60s 2.62s p=0.932 n=6
Total Time 13.88s (± 0.24%) 13.85s (± 0.46%) ~ 13.77s 13.93s p=0.470 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,373k (± 0.01%) 347,372k (± 0.00%) ~ 347,350k 347,398k p=0.575 n=6
Parse Time 2.46s (± 0.42%) 2.46s (± 0.40%) ~ 2.45s 2.47s p=0.933 n=6
Bind Time 0.93s (± 0.56%) 0.92s (± 0.56%) ~ 0.92s 0.93s p=0.311 n=6
Check Time 6.91s (± 0.56%) 6.91s (± 0.51%) ~ 6.87s 6.96s p=1.000 n=6
Emit Time 4.05s (± 0.19%) 4.05s (± 0.20%) ~ 4.04s 4.06s p=0.729 n=6
Total Time 14.35s (± 0.26%) 14.35s (± 0.23%) ~ 14.30s 14.40s p=1.000 n=6
TFS - node (v18.15.0, x64)
Memory used 302,633k (± 0.00%) 302,638k (± 0.01%) ~ 302,618k 302,662k p=0.688 n=6
Parse Time 1.99s (± 1.66%) 2.00s (± 0.86%) ~ 1.98s 2.03s p=0.686 n=6
Bind Time 1.00s (± 0.75%) 1.01s (± 1.20%) ~ 0.99s 1.02s p=0.240 n=6
Check Time 6.26s (± 0.46%) 6.28s (± 0.41%) ~ 6.25s 6.32s p=0.124 n=6
Emit Time 3.59s (± 0.48%) 3.58s (± 0.42%) ~ 3.56s 3.60s p=0.279 n=6
Total Time 12.84s (± 0.39%) 12.87s (± 0.26%) ~ 12.82s 12.91s p=0.295 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,630k (± 0.02%) 470,642k (± 0.01%) ~ 470,600k 470,681k p=0.378 n=6
Parse Time 2.57s (± 0.63%) 2.57s (± 0.79%) ~ 2.55s 2.60s p=0.867 n=6
Bind Time 0.99s (± 1.22%) 0.98s (± 0.56%) ~ 0.98s 0.99s p=0.235 n=6
Check Time 16.74s (± 0.49%) 16.76s (± 0.28%) ~ 16.70s 16.83s p=0.375 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.30s (± 0.50%) 20.32s (± 0.27%) ~ 20.25s 20.41s p=0.372 n=6
xstate - node (v18.15.0, x64)
Memory used 511,983k (± 0.01%) 511,891k (± 0.01%) -91k (- 0.02%) 511,862k 511,941k p=0.008 n=6
Parse Time 3.27s (± 0.23%) 3.27s (± 0.36%) ~ 3.25s 3.28s p=0.796 n=6
Bind Time 1.54s (± 0.00%) 1.55s (± 0.26%) +0.01s (+ 0.54%) 1.54s 1.55s p=0.007 n=6
Check Time 2.79s (± 0.67%) 2.77s (± 0.95%) ~ 2.74s 2.80s p=0.252 n=6
Emit Time 0.08s (± 0.00%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=0.405 n=6
Total Time 7.68s (± 0.27%) 7.67s (± 0.35%) ~ 7.64s 7.70s p=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)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

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

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

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

Everything looks good!

@ahejlsberg
Copy link
Member Author

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

type Keys<P> = { [K in keyof P]: K }[keyof P];

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

type Foo = {
  a?: string;
  b: number;
}

type FooKeys = 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:

type Keys<P> = { [K in keyof P]-?: K }[keyof P];

With this change there are no new errors in enzyme.

@gabritto
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 28, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 28, 2023

Hey @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".;

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.

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 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
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants