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

Is it possible to get an intersection type of Record from keys and values as tuples or unions that is one-to-one?

Given the following tuples: (as an example. I could swap to string unions if that would make it possible.)

type Keys = ['North', 'East', 'South', 'West'];
type Values = ['n', 'e', 's', 'w'];

How can I get something similar to this:

type OneToOne<
  K extends [...(string | number | symbol)[]],
  V extends [...unknown[]]
> = Record<K[0], V[0]>
  & Record<K[1], V[1]>
  & Record<K[2], V[2]>
  & Record<K[3], V[3]>;

But for any arbitrarily long tuples and or unions. Whether K and V are tuples or string unions in the end, doesn’t make that much of a difference to me. I suppose they would just need to be the same length.

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

>Solution :

You can use a mapped type to iterate over the keys of Keys, filtering out only number keys ("0", "1", etc). Then you can use this number key to index into Keys for the property type (in the as clause of the mapped type) and get the corresponding value out of Values:

type Keys = ['North', 'East', 'South', 'West'];
type Values = ['n', 'e', 's', 'w'];

type KetValues<K extends PropertyKey[], V extends Record<keyof K & number, string>> = {
  [P in keyof K as P extends `${number}` ? K[P] & PropertyKey : never]: V extends Record<P, infer Prop> ? Prop: never
}

type x = KetValues<Keys, Values>
// type x = {
//     North: "n";
//     East: "e";
//     South: "s";
//     West: "w";
// }

Playground Link

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