-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Conversation
@typescript-bot test top100 |
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! |
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! |
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! |
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 |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
@ahejlsberg Here they are:
CompilerComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@ahejlsberg Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Something interesting changed - please have a look. Details
|
Hey @ahejlsberg, the results of running the DT tests are ready. Branch only errors:Package: enzyme
|
@ahejlsberg Here are the results of running the top-repos suite comparing Everything looks good! |
The new error in the Definitely Typed type Keys<P> = { [K in keyof P]: K }[keyof P]; In the However, upon instantiation with a type that contains optional properties, the resulting type will actually include type Foo = {
a?: string;
b: number;
}
type FooKeys = Keys<Foo>; // "a" | "b" | undefined Since The issue here really is that the constraint of Generally, types like type Keys<P> = { [K in keyof P]-?: K }[keyof P]; With this change there are no new errors in |
@typescript-bot pack this |
Hey @gabritto, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
There was a problem hiding this 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.
…S intersection constrained types More details: microsoft/TypeScript#56515
With this PR we reduce intersections of constrained type variables and primitive types as follows:
T & P
orP & T
, whereT
's constraint is a subtype ofP
, is reduced to justT
. For example, givenT extends "a" | "b"
, the intersectionT & string
is reduced to justT
.T & P
orP & T
, where no constituent ofT
's constraint is a subtype ofP
, andP
is not a subtype ofT
's constraint, is reduced tonever
. For example, givenT extends "a" | "b"
, the intersectionT & number
is reduced tonever
.T extends "a" | "b"
, the union typeT & "a" | T & "b"
is reduced to justT
.