-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?

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291

1 Answers1

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
}
André Krosby
  • 1,008
  • 7
  • 18