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

Empty intersection reduction #31838

Merged
merged 7 commits into from
Jun 11, 2019
Merged

Empty intersection reduction #31838

merged 7 commits into from
Jun 11, 2019

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Jun 9, 2019

With this PR we reduce empty intersections to never immediately upon construction. For example, instead of preserving the type string & number and only reducing it away in union types we now immediately reduce it to never.

Some examples of types that now immediately reduce to never:

type T1 = 'a' & 'b';  // never
type T2 = { a: string } & null;  // never in strict mode, null in non-strict mode
type T3 = { a: string } & undefined;  // never in strict mode, undefined in non-strict mode
type T4 = string & number;  // never
type T5 = number & object;  // never
type T6 = symbol & string;  // never
type T7 = void & string;  // never

In #18438 we chose to preserve empty intersection types (a) to make it easier to understand how they originated and (b) to partially preserve the ability to "tag" primtive types using enum types (e.g. string & Tag where Tag is an enum). In practice this doesn't seem to outweigh the confusion these types bring about, in particular because there are subtle behavioral differences between never and types like string & number even though they really should be the same. The differences include:

  • never is assignable to anything whereas string & number is assignable only to string or number.
  • any is not assignable to never but is assignable to string & number.
  • For a type T with no index signatures, T[never] resolves to never whereas T[string & number] is an error.

These differences have no practical value. Particularly confusing is that string & boolean reduces to never (because it normalizes to string & true | string & false, which then reduces to never), whereas string & number sticks around and behaves differently.

Fixes #31663.

@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 9, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 015f4c5. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 9, 2019

Heya @ahejlsberg, I've started to run the extended test suite on this PR at 015f4c5. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg
Copy link
Member Author

RWC test run is clean. DT test run has one minor difference in ember/test/detect-instance.ts. Types such as null & { foo: string } and undefined & { foo: string } now reduce to never but previously would stick around (yet disappear in union types) and allow properties to be accessed. The new behavior is definitely more consistent.

@jack-williams
Copy link
Collaborator

Does this still support branding via object literals?

type Tag<T> = T & { readonly brand: unique symbol };
type TaggedNumber = Tag<number>; // not never

@ahejlsberg
Copy link
Member Author

@jack-williams It does, except you can't brand null or undefined. You sort of could before, but only in a limited sense. The minute you put null & { brand: xxx } in a union type it would disappear.

@ahejlsberg
Copy link
Member Author

ahejlsberg commented Jun 9, 2019

@jack-williams The only thing that changes with this PR is when we reduce away empty intersections. We now do it upon construction but we used to do it only when an intersection was put in a union type. The actual patterns we reduce away (or don't reduce away) remain the same.

@jack-williams
Copy link
Collaborator

The only thing that changes with this PR is when we reduce away empty intersections.

Ah, that makes it clearer. Thank you!

if (type.flags & TypeFlags.Unit && includes & TypeFlags.Unit) {
// We have seen two distinct unit types which means we should reduce to an
// empty intersection. Adding TypeFlags.NonPrimitive causes that to happen.
includes |= TypeFlags.NonPrimitive;
Copy link
Member

Choose a reason for hiding this comment

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

Counterexample:

enum A {
  Zero = 0,
  One = 1
}

type Zeroish<T> = T & 0;
type ShouldNotBeNever = Zeroish<A.Zero> | never;

we handle this incorrectly in master already - I'm just not a fan of propagating the logical error.

Choose a reason for hiding this comment

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

So is the reason for that that the code highlighted considers A.Zero and 0 to be distinct? I'm just trying to ascertain where in that example above the never creeps in.

Copy link
Member Author

Choose a reason for hiding this comment

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

@weswigham I think it is debatable whether we want to consider literals and literal enum members with the same underlying value to be intersectable. But either way, it is an issue that should be covered in a separate PR. Here we're just concerned with changing when we reduce, not how.

@ahejlsberg
Copy link
Member Author

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 11, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 015f4c5. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg
Copy link
Member Author

RWC and DT test runs are both clean now.

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 expect we'll get some reports about the using-an-enum-to-tag break upon release - we should remember to include this in the breaking change log.

@dharnil
Copy link

dharnil commented May 31, 2024

Hi I wanted to check to see if this has already been resolved as I am still facing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type 'any' is not assignable to type 'never' with boolean in interface in TS 3.5
6 participants