-
-
Notifications
You must be signed in to change notification settings - Fork 545
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 Nullish
type
#318
Add Nullish
type
#318
Conversation
What would you use it for? I'm not sure I want to add this as I generally try to encourage people to move away from using |
It's only really useful when you're treating function nullish(value: unknown): value is Nullish {
return value === null || value === undefined;
}
const data = [1, null, 2, undefined, 3];
if (data.some(nullish)) {
throw new TypeError('bad data');
}
data.reduce((a, b) => a + b); |
I'm unfortunately going to pass on this type. |
I dont think, it makes sense to move away from null and just rely on undefined. From graphql persepective, having null as explicit value for some input would mark it as deleted and leaving it as undefined would mean no updated happened. So as graphql user, we rely on the differentiation between null and undefined. |
@sadokmtir There are often better ways to solve problems like this than to use |
@sindresorhus Can you show me an example or point me to some resources that show what you are proposing ? Because I don't think there are better solutions, otherwise this won't be requested to add it into the graphql-spec. Saying that other devs are misusing null is never an argument. As inexperienced could misuse anything. So using null for all kind of purposes in a wild is never a valid reason. Null points to something intentional set and undefined means the value was not provided. Those rules are mostly valid to all programming languages. |
I strongly agree with @sadokmtir. I wish JavaScript didn't have both null and undefined (like Ruby has only nil), but since we DO have null and undefined, just ignoring one isn't very helpful. Further, JSON supports both null and undefined conceptually. Null is a critical part of the JSON schema. The canonical way to use null and undefined correctly in JavaScript is as @sadokmtir says:
It's a common pattern in APIs with an As such, I'd add my support to requesting that 'type-fest', and awesome library BTW, to support ways of working with types that are "nullish" (don't care what the actual word is) - that are either null or undefined. |
Implements a type for the nullish values described in the ECMAScript Language Specification:
(source)