Comments (9)
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;
}
from remeda.
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 optional()
object properties return a type which has undefined
in it.
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);
from remeda.
Your example could be shortened to:
const hasFirstName = user === undefined ? false : isEmpty(user.firstName);
not exactly because the firstName
itself is also optional.
Because remeda isn't configurable, we need to make a decision on how strict our types should be.
we always have the option of adding stricter types to the .strict
namespace?
from remeda.
true, but that doesn't address what should be the default returned value for isEmpty(undefined), is it true or false?
from remeda.
I think it should be true
, given that we agree that undefined
and null
can both be considered empty
(for whatever empty
means in that terminology 😅 )
from remeda.
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/
https://docs.hhvm.com/hsl/reference/function/HH.Lib.Str.is_empty/
from remeda.
yeah I would also not allow null for collection-only methods like flatMap
(I think we had a separate discussion about this), but here, were strings are also allowed, I think it would be a better DX.
from remeda.
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: isEmpty(obj["typo"])
from remeda.
🎉 This issue has been resolved in version 1.26.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from remeda.
Related Issues (20)
- Allow `conditional.defaultCase` to infer its return type HOT 3
- Incorrect type assertion for isIncludedIn HOT 5
- Add optional `obj` parameter to `Evolver`? HOT 4
- Add `invariant` HOT 2
- Add `some` and `every`? HOT 16
- [Feature Request]: Support Maps in isDeepEqual HOT 6
- Add `sortedUniq` HOT 6
- Further improve `stringToPath` types; consolidate `setPath` and `pathOr`? HOT 5
- isDeepEqual and negation in guard can break type HOT 6
- Typing issue when using `isDeepEqual` in `differenceWith` function HOT 2
- Why is fromKeys typed with Partial<>? HOT 3
- `groupBy` has broken types HOT 3
- Test against Typescript nightlies HOT 2
- [Feature Request] Remeda ESLint Plugin HOT 7
- Add mergeAndConcat function HOT 1
- add mapEntries function HOT 4
- Add "R" or "Remeda" globally to the docs website for easy experiments HOT 4
- Add `mapAccum` HOT 5
- Unexpected types when using isNullish/isNonNullish in conditional HOT 1
- Incorrect type assertion in negative case for hasSubObject HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remeda.