-1

Here is my current code:

interface ObjectIsEmptyProps {
  [index: string]: number | string;
}
export const objectIsEmpty = (a: ObjectIsEmptyProps) => a &&  
Object.keys(a).length > 

I need to check an object that might have any number properties provided to it. So 3 examples of possible function calls are:

objectIsEmpty({}) //true
objectIsEmpty({ jamie: 'hutber' }) //false
objectIsEmpty({fank: 'skinner', jamie: 'hutber' }) //false
objectIsEmpty({anArray: [], aBoolie: false, chickenSkin: 'isGreat'}) //false

So my function can take an object with an undetermined set of properties. Currently the only way I can TS to be happy is to use any as the type of the argument.

How can I support any number of object properties without using any?

5
  • Thanks for taking the time to look at the question! Could I please get some feedback on how to improve the question after the downvote. Thanks Commented Jun 22, 2022 at 7:37
  • How about !Object.keys(a).length != 0 Commented Jun 22, 2022 at 7:41
  • The issue will still exist that I cannot define the types of the function to accept any shaped object. Commented Jun 22, 2022 at 7:44
  • Not sure what you mean by that, however here are some ways stackoverflow.com/questions/2673121/… Commented Jun 22, 2022 at 7:45
  • Have updated with more clear examples of what I need it to do. Commented Jun 22, 2022 at 19:41

1 Answer 1

1

I'm not sure I fully understand the question. However, could you use the Record utility?

export const objectIsEmpty = (a: Record<string, unknown>) => {
  return Object.keys(a).length === 0
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.