-
Notifications
You must be signed in to change notification settings - Fork 130
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
isEmpty doesn't accept falsy input #369
Comments
here's a diff that I think would accomplish this: diff --git a/src/isEmpty.ts b/src/isEmpty.ts
index b42b70c..fc73d57 100644
--- a/src/isEmpty.ts
+++ b/src/isEmpty.ts
@@ -22,6 +22,7 @@ export function isEmpty(data: ReadonlyArray<unknown> | []): data is [];
@@ -22,6 +22,7 @@ export function isEmpty(data: ReadonlyArray<unknown> | []): data is [];
export function isEmpty<T extends Readonly<Record<PropertyKey, unknown>>>(
data: T
): data is Record<keyof T, never>;
+export function isEmpty(data: null | undefined): true;
export function isEmpty(data: unknown): boolean {
if (isArray(data) || isString(data)) {
return data.length === 0;
@@ -29,5 +30,5 @@ export function isEmpty(data: unknown): boolean {
if (isObject(data)) {
return Object.keys(data).length === 0;
}
- return false;
+ return data == null;
} |
When I reviewed the original PR my fear was that any relaxation of typing would allow some bugs to fall through, this is the reasoning behind typescripts strictest configs which are not enabled by default (like exactOptionalPropertyTypes). Zod have the same issue where Because remeda isn't configurable, we need to make a decision on how strict our types should be. I prefer strictness, but understand this isn't everyone's cup of tea. Your example could be shortened to: const hasFirstName = user === undefined ? false : isEmpty(user.firstName); and I think that's descriptive enough, think about a different use-case. It's not always obvious what the undefined case should return, it's not always going to be false. const hideWelcomeGreeting = user === undefined ? true : isEmpty(user.firstName); |
not exactly because the
we always have the option of adding stricter types to the |
true, but that doesn't address what should be the default returned value for isEmpty(undefined), is it true or false? |
I think it should be |
Facebook's Hack standard library define null as true for strings (and strings only) and doesn't accept nulls for collections. I don't mind going with this... https://docs.hhvm.com/hsl/reference/function/HH.Lib.C.is_empty/ |
yeah I would also not allow null for collection-only methods like |
Checkout the #377 that solves this. My only issue is that we will now accept isEmpty(undefined) too, and I couldn't find a way around it so users lose some protection against cases like: |
🎉 This issue has been resolved in version 1.26.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
we ran into this issue today, consider the following:
this errors because we can't pass
undefined
toisEmpty
(TypeScript playground). One workaround is to either check upfront explicitly:or coerce to empty string:
both of which are kinda weird. Imo, "a function that checks if the passed parameter is empty" (from the docs) could consider accepting
null
andundefined
and returntrue
for them being "empty".What do you think @eranhirsch ?
The text was updated successfully, but these errors were encountered: