-
-
Notifications
You must be signed in to change notification settings - Fork 113
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 for is.truthy
and is.falsy
#151
Add type guard for is.truthy
and is.falsy
#151
Conversation
I think it would be better to participate in #146 first. There are unanswered questions there.
|
It doesn't look like you ran the tests before submitting. |
83b0b02
to
dfd6d4e
Compare
dfd6d4e
to
12a6161
Compare
You're right, sorry about that, updated with passing tests. Also added
I don't follow what "restrictions on using is.truthy() as a type guard function" means so not sure how to add to the conversation there. I also don't know "in what cases would [is.falsy as a typeguard] be useful". But |
// @younho9 |
My comment about |
I thought export type Falsy = false | 0 | 0n | '' | null | undefined;
const isTruthy = <T>(value: T | Falsy): value is T => Boolean(value);
const isFalsy = (value: unknown): value is Falsy => !Boolean(value);
declare const number_: number;
if (isTruthy(number_)) {
number_ // number
} else {
number_ // never
}
if (isFalsy(number_)) {
number_ // 0
} else {
number_ // number
}
I think using it with a filter could be one of use case. |
Also, when value has falsy type, declare const falsyValue: null;
if (isTruthy(falsyValue)) {
falsyValue // null, expected never
} else {
falsyValue // never, expected null
}
if (isFalsy(falsyValue)) {
falsyValue // null
} else {
falsyValue // never
} |
I use
I can guess from the defaults in unicorn that @sindresorhus is not a fan of passing eg |
Ah, I get it now. So it's really just about narrowing away |
Do you know why that happens? |
Seems like this change could be useful even with the mentioned limitations. |
@zshannon Could you also apply this to |
is.truthy
to narrow resultis.truthy
and is.falsy
Fixes #146 and not sure if adding the same Falsy guard to
is.falsy
helps there because I'm just usingis.truthy
as a.filter
but happy to add theFalsy
annotation tois.falsy
too if helpful