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

Make isEmpty a type guard #16

Open
yokuze opened this issue May 7, 2020 · 2 comments
Open

Make isEmpty a type guard #16

yokuze opened this issue May 7, 2020 · 2 comments

Comments

@yokuze
Copy link
Contributor

yokuze commented May 7, 2020

Currently, isEmpty just returns a boolean. It would be much more useful if it were a type-guard that asserted that its parameter was one of the "empty" object types its checking for.

@crgwbr
Copy link
Contributor

crgwbr commented Jun 22, 2021

@yokuze This is easy for null, undefined, array, and string types. But isEmpty currently also works for the IArguments type, which isn't generic. So, there is no type for empty arguments vs. non-empty arguments. So… thoughts?

@yokuze
Copy link
Contributor Author

yokuze commented Jun 22, 2021

I don't think we need to differentiate between empty vs non-empty IArguments in the type guard assertion. It would be nifty if there were an existing way to do so, but I don't think it'll be that helpful to us to make that distinction.

By far, the most frequent pain point that we are trying to solve by making this function a type guard is something like this:

function doSomething(withThis: string | undefined): void {
   if (isEmpty(withThis)) {

   } else {
      // TypeScript should at least know here that `withThis` not `undefined`
   }
}

Anything beyond that is extra credit IMO.

That said, I may be missing something, so let me know if there's a use case you think we should support by differentiating between empty vs non-empty IArguments. If there is, the only option I can think of right now would be to extend the IArguments type like:

interface IEmptyArguments extends IArguments {
   length: 0;
}

crgwbr added a commit to crgwbr/toolbox that referenced this issue Jun 22, 2021
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jun 22, 2021
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jun 22, 2021
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jun 22, 2021
crgwbr added a commit to crgwbr/toolbox that referenced this issue Mar 1, 2022
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jul 23, 2024
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jul 23, 2024
crgwbr added a commit to crgwbr/toolbox that referenced this issue Jul 23, 2024
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

No branches or pull requests

2 participants