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

Proposal: is.falsy() as a type guard #146

Closed
younho9 opened this issue Dec 9, 2021 · 3 comments · Fixed by #151
Closed

Proposal: is.falsy() as a type guard #146

younho9 opened this issue Dec 9, 2021 · 3 comments · Fixed by #151

Comments

@younho9
Copy link

younho9 commented Dec 9, 2021

Although there are restrictions on using is.truthy() as a type guard function, is.falsy() could be a type guard function.

type Falsy = false | 0 | 0n | '' | null | undefined;

is.falsy = (value: unknown): value is Falsy => !value;
@sindresorhus
Copy link
Owner

Sure, it could, but in what cases would it be useful though? The narrowing is still pretty wide.

@younho9
Copy link
Author

younho9 commented Dec 9, 2021

Your point is correct.

If change signature to use Extract, it would be useful for extracting in a Falsy type from a given value type.

type Falsy = false | 0 | 0n | '' | null | undefined;

is.falsy = <T>(value: T): value is Extract<Falsy, T> => !value;

But, I found there are some bug in extracting from union type.

microsoft/TypeScript#31156

TypeScript Playground

If this problem is solved, It would be useful to use is.falsy as a type guard function, but this problem seems difficult to solve.

So, if you think this type of guard is unnecessary, I will close the issue.

@sindresorhus
Copy link
Owner

We can keep the issue open for now to gather more feedback and also wait for the TS issue.

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

Successfully merging a pull request may close this issue.

2 participants