jfairbank / combos

Generate all possible permutations of an object's key-value pairs
MIT License
27 stars 2 forks source link

Add Typescript definitions. #2

Open azizhk opened 5 years ago

azizhk commented 5 years ago

Just thought I'd share types that I wrote for this package, in case someone is looking for it.

declare module "combos" {
  export const UNDEF:Symbol;

  interface Input {
    [key: string]: any[]
  }

  export default function combos<T extends Input>(inp: T): {
    [P in keyof T]: T[P][0]
  }[];
}
screen shot 2018-10-31 at 3 19 06 pm

Not creating a PR because I could not use UNDEF to show that a particular key could be undefined in the output. Will create a PR when I have a better solution. Just leaving this here if someone needs it for types without using UNDEF.

tvler commented 5 years ago

@azizhk here's another way to infer the type of an array!

export default function combos<T extends Input>(inp: T): {
  [P in keyof T]: T[P] extends (infer ElementType)[] ? ElementType : any;
}[];

Found this repo after implementing my own solution in ts here https://github.com/tvler/prop-sets