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

Add Nullish type #318

Closed
wants to merge 1 commit into from
Closed

Add Nullish type #318

wants to merge 1 commit into from

Conversation

jonahsnider
Copy link
Contributor

Implements a type for the nullish values described in the ECMAScript Language Specification:

“nullish” values (null or undefined)

(source)

@sindresorhus
Copy link
Owner

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 null whenever possible: sindresorhus/meta#7

@jonahsnider
Copy link
Contributor Author

It's only really useful when you're treating null or undefined as "missing value" and don't want to write null | undefined over and over.

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);

@sindresorhus
Copy link
Owner

I'm unfortunately going to pass on this type.

@jonahsnider jonahsnider deleted the feat/nullish branch November 30, 2021 18:18
@sadokmtir
Copy link

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.
You can see the proposal here:
graphql/graphql-spec#476

So as graphql user, we rely on the differentiation between null and undefined.

@sindresorhus
Copy link
Owner

@sadokmtir There are often better ways to solve problems like this than to use null. sindresorhus/meta#7 (reply in thread)

@sadokmtir
Copy link

@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.

@Xample Xample mentioned this pull request Jan 26, 2023
@shanebdavis
Copy link

shanebdavis commented Nov 17, 2024

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:

"null points to something intentionally set and undefined means the value was not provided."

It's a common pattern in APIs with an update method to allow support passing values with null to clear the values in the database OR support not passing the values at all (e.g. undefined) to not update them.

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.

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 this pull request may close these issues.

4 participants