Create a type Map<keyof T, boolean> from object

I have a typed function that’s almost perfect. Given a generic how would I allow for the following type for Argument that’s currently invalid?

type Argument<T> =
  | keyof T
  | (keyof T)[]
  | Record<keyof T, any> // <- Not working

export function classNames<T>(style) {
  const handleArgs = (args: Argument<T>[]) => {}
  return (...args: Argument<T>[]) => handleArgs(args)
}

const exampleObject = { prop1: {}, prop2: 2 }
const myFunc = classNames<typeof exampleObject>(exampleObject)
myFunc('prop1') //valid
myFunc('prop1', ['prop2']) //valid
myFunc('prop1', ['prop2']) //valid
myFunc('prop1', ['prop2'], { prop2: 2 }) //invalid

>Solution :

Go for partial so you don’t need everything everytime:

export function classNames<T>(style: T) {
  const handleArgs = (args: Partial<Argument<T>>[]) => {};
  return (...args: Partial<Argument<T>>[]) => handleArgs(args);
}

Leave a Reply