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

[feature request] add new isNullish predicate function #75

Open
DerekNonGeneric opened this issue Nov 5, 2022 · 4 comments
Open

[feature request] add new isNullish predicate function #75

DerekNonGeneric opened this issue Nov 5, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@DerekNonGeneric
Copy link
Member

Checks whether something is null or undefined.

Would be useful for OpenINF/openinf-util-object#133. We can have it out in the next minor…

Can this be achieved solely using Nullish coalescing? Maybe in the TS, but now wondering what ES year we are searching for compatibility with since this is a relatively new language feature.

Refs: https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-intersection-reduction-union-compatibility-and-narrowing

/cc @septs @ignoreintuition

@DerekNonGeneric DerekNonGeneric added enhancement New feature or request good first issue Good for newcomers labels Nov 5, 2022
@DerekNonGeneric
Copy link
Member Author

DerekNonGeneric commented Nov 5, 2022

Good references, and pretty much identical, but the naming bikeshedding might have already taken place in a little Twitter poll i ran a few months ago. Only like a handful of people voted, so it doesn't really mean much, but good arguments about the ES spec “cementing” the word to have that meaning (if that is a bad reason for nullish, still open to suggestions)!

Do you not like the -ish suffix? It makes it sound vague when we really need to be exact here.

Suffix

  1. Typical of, similar to,
    being like.

    • 1859, (as Holme Lee), Against Wind and Tide, volume 1, p.
      273
      :

      {{...}}; for she had recently developed a magpie**[-]ish**
      tendency to appropriate and conceal trifling matters;{{...}}

  2. Somewhat, rather.

  3. About,
    approximately.

    (Sometime around ten.)

  4. Of, belonging{{,}} or relating to (a
    nationality, place, language or similar
    association with something).

Usage notes
  • This is a productive termination used as a regular formative of
    adjectives (which are sometimes also used as nouns).

  • Many of the words may have a more or less depreciative or contemptuous
    force.

  • This is the regular formative of patrial
    adjectives, with the suffix in some adjectives being contracted to
    -sh or (especially when t precedes) to -ch, as in Welsh
    (formerly also Welch), Scotch, Dutch{{,}} and French. Some
    used colloquially or made up on occasion may have a diminutive or

Refs: https://en.wiktionary.org/wiki/-ish

@DerekNonGeneric
Copy link
Member Author

One would think that with the word Nullish essentially having been canonized in the MDN Glossary, as it has been, this should pretty much be a settled debate (was unable to find that specific word in any ES spec).

Then you have a beauty like this from the TS 4.8 release post linked above:

function throwIfNullable<T>(value: T): NonNullable<T> {
    if (value === undefined || value === null) {
        throw Error("Nullable value!");
    }

    // Used to fail because 'T' was not assignable to 'NonNullable<T>'.
    // Now narrows to 'T & {}' and succeeds because that's just 'NonNullable<T>'.
    return value;
}

Where they'd clearly have called this function isNullable() instead if they were to pull it out on its own.


isNullable() would sound very TypeScript-ish and perhaps might be more appropriate for the typescript function annotations.

isNullLike() seems like it would sound very Closure-ish (iirc there is an isMapLike somewhere).

@DerekNonGeneric
Copy link
Member Author

  • isNullable( value )

and

  • isNullish( value )

and

  • isNil( value )

… all sound fine to me, but the fact that the most official JS documentation (MDN web docs) use Nullish is kind of a big deal, and not so sure TS or Lodash have as much credibility (loads of people will likely reference the MDN glossary, while TS can change their mind about things (and often does) between releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants