Skip to content

Discriminating unions based on enum seem to bug out depending on the size of the enum #60198

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

Closed
mdaobt opened this issue Oct 11, 2024 · 2 comments

Comments

@mdaobt
Copy link

mdaobt commented Oct 11, 2024

🔎 Search Terms

The issue seems that the function getMe() is not able to deal with discriminating unions.

Image

However, it stops if I trim the enum and reduce the amount of values. Try removing everything after AA and the issue disappears.

It looks like a very convoluted example, but we really want this to work in our project.

My question is, why is the problem occurring?

🕗 Version & Regression Information

  • This changed between versions 5.5.4 and 5.6.3
  • This changed in commit or PR (I have no idea)
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about (I have no idea)
  • I was unable to test this on prior versions because it seems to be happening even now

⏯ Playground Link

Link can be found here

💻 Code

export enum EquipmentType {
  A,
  B,
  C,
  D,
  E,
  F,
  G,
  H,
  J,
  K,
  L,
  M,
  N,
  O,
  P,
  R,
  Q,
  W,
  Z,
  AA,
  AB,
  AC,
  AD,
  AE,
  AF,
  AG,
  AH,
}

export type AssetCommonData<
  T extends EquipmentType = EquipmentType
> = {
  equipmentType: T;
};

export type A = AssetCommonData & {
  equipmentType: typeof EquipmentType.A;
};

export type B = AssetCommonData & {
  equipmentType: typeof EquipmentType.B;
};

export type C = AssetCommonData & {
  equipmentType: typeof EquipmentType.C;
};


export type D = AssetCommonData & {
  equipmentType: typeof EquipmentType.D;
};

export type E = AssetCommonData & {
  equipmentType: typeof EquipmentType.E;
};

export type F = AssetCommonData & {
  equipmentType: typeof EquipmentType.F;
};

type SpecificAssetData = A | B| C | D | E |F;

type AssetData = SpecificAssetData | AssetCommonData<Exclude<EquipmentType, SpecificAssetData['equipmentType']>>;

const getMe = (equipmentType: EquipmentType): AssetData => {
    return {
        equipmentType,
    };
}

🙁 Actual behavior

Typecheck on getMe() is failing . If you remove everything after AA, the issue disappears. As if the size of the original enum affects what Typescript does.

Image

🙂 Expected behavior

getMe() should not be failing.

Additional information about the issue

Playground link: https://www.typescriptlang.org/play/?#code/FAUwHgDg9gTgLgAhAOwK4FsEFECOqCWE6KcAKgJ4QgIDewCCAggDT0IBCrDAwlwgCJ8sfAGJ8A4nwASfAFJ8A0nwAyfALJ8AcnwDyfAAp8ASnwCKfAOp8AWn0Ys2jTo96PBj4Y7GPJjmcABfYFBIWEQ4SmpGAGdokDhuKHR0KGR+AEM4dIAeNlIkMDgUABNo7DxCYmQySIQAXnKCIhIKKmAAPnraNhAK5urWkAAuBFIAbkCJkOh4BAiqJi6YuISklLTM9IQAMm6GXqaqmqoR+ZAoADNGypbIgDpGCYCp8Bnw2vYl2PjE5NSMrI7PZIPpHQanSKXa79Y4gO7sJ4vUKzM4IbhfFa-dYAra7Oj7UG3E5zSFXXCHIlw7iI4LTMIkhb8DE-Nb-TZA-EgikDSIQqhQ8k3HlUO78Gl0lG1LDM1Z-DaAvE9QnC4YM85k5Wwu5YcWvemokQyrFshXAg5C2F89XQsH3ETi1EAZSoAGN8Bd8C7lvEcUsEAAfDiB9GBpmB6X++3BVHeuC+hrOkBuj1e75x9mB2PG+U5LBgF0AG1QxRA2UFMMGzAQieTntjOIA2gByc0VyJNgC67XaUxdqWiiAA5vE1NQGgAKVu24nl6cgACUI3r7LqnU5DBg8VQMGQwIY+65FsrbAYz0CQA

@MartinJohns
Copy link
Contributor

  • This changed between versions 5.5.4 and 5.6.3

What makes you think so? The playground shows no difference between those two versions.

Duplicate of #43283. Amount of elements in a union is (intentionally) limited.

@mdaobt
Copy link
Author

mdaobt commented Oct 11, 2024

Ah ! I can see why it was confusing. I was just filling out the template. I tested only in 2 versions.

@mdaobt mdaobt closed this as completed Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants