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

Regard extended enum as exclusive union type of its instances #7

Open
inhibitor1217 opened this issue Dec 5, 2021 · 1 comment
Open
Assignees

Comments

@inhibitor1217
Copy link
Owner

Native TypeScript enums are exclusive union of each members.

enum Fruit { Apple, Orange }

type T = Fruit.Apple & Type.Orange // never

This allows type interpretations in some use-cases to be useful:

declare const fruit: Fruit;

if (fruit === Fruit.Apple) { ... }
if (fruit === Fruit.Orange) { ... }
// now, compiler knows this code is not reachable
declare const fruit: Fruit;

switch (fruit) {
  case Fruit.Apple: { ..., break; }
  case Fruit.Orange: { ..., break; }
  // now, compiler knows this code is not reachable
}

Allow this functionality to extended enums, so that the following holds:

enum _Fruit { Apple, Orange }
const Fruit = extend<typeof _Fruit, _Fruit>(_Fruit);

type T = Fruit.Apple & Fruit.Orange; // never

Also, the extended enum class itself and the members should be type and value at the same time, like the native enum does.

type T = Fruit.Apple extends Fruit ? true : false // true

enum _Vegetable { Potato, Celery }
const Vegetable = extend<typeof _Vegetable, _Vegetable>(_Vegetable);

type U = Vegetable.Potato extends Fruit ? true : false // false
@inhibitor1217
Copy link
Owner Author

The exclusivity of instance types are implemented in #22.
The next goal is to make the union of instance types to exhaustively define the whole type.

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

1 participant