Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading