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

Add type guard to 'is' and 'is.not' methods #27

Closed
inhibitor1217 opened this issue Dec 11, 2021 · 1 comment
Closed

Add type guard to 'is' and 'is.not' methods #27

inhibitor1217 opened this issue Dec 11, 2021 · 1 comment
Assignees

Comments

@inhibitor1217
Copy link
Owner

As each instances are defined as branded type of the extended enum (#7), type guards should be supported.

enum _Fruit { Apple, Orange }

class Fruit extends extend<typeof _Fruit, _Fruit>(_Fruit) {}

declare const f: Fruit;

if (f === Fruit.Apple) {
  type T = typeof f; // should be Fruit.Apple, it is not working now regardless of type guards
}

if (f.is(Fruit.Apple)) {
  type T = typeof f; // Fruit.Apple
}

if (f.is.not(fruit.Apple)) {
  type T = typeof f; // Exclude<F, Fruit.Apple> or something similar
}
@inhibitor1217 inhibitor1217 self-assigned this Dec 11, 2021
@inhibitor1217
Copy link
Owner Author

inhibitor1217 commented Dec 20, 2021

Apparently, type guards by nested property is not supported in current version of TypeScript. (related pull request - microsoft/TypeScript#38839)

Therefore, providing a type guard for is.not is not possible right now.

We propose an alternative to use isNot as an alias of is.not. isNot lives as a direct property of ExtendedEnum, hence it can be used for type narrowing.

is.not will continue to be supported, but type guards will not be provided.

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