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

How to write if condition to exclude EmptyObject #732

Closed
simPod opened this issue Oct 26, 2023 · 2 comments
Closed

How to write if condition to exclude EmptyObject #732

simPod opened this issue Oct 26, 2023 · 2 comments

Comments

@simPod
Copy link

simPod commented Oct 26, 2023

I wonder how you guys do it.

I've been using Record<string, never> to represent {} but wanted to migrate to TypeFest's EmptyObject

I've been using Object.keys(foo).length === 0 to detect the empty object type but that does not work for EmptyObject. Is there a way to check for it without typecasting or creating custom typeguard?

var foo: EmptyObject | Record<string, number>;

if(Object.keys(foo).length === 0) {
   // EmptyObject here
} else {
  // Record<string, number> here
}

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

You need to use a check function with a better type-guard, like: https://github.com/sindresorhus/is#emptyobjectvalue

@simPod
Copy link
Author

simPod commented Oct 26, 2023

Ok I did not know https://github.com/sindresorhus/is. Thanks so much for that!

@simPod simPod closed this as completed Oct 26, 2023
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