Home > Mobile >  How to index a specific array element in Typescript?
How to index a specific array element in Typescript?

Time:06-01

I want to create a type that combines the values of two properties of one object in an array.

My solution so far, looks like this:

const CONFIGS = [
  { section: "a", name: "1" },
  { section: "b", name: "2" },
] as const;

type ConfigSections<I extends number> = typeof CONFIGS[I]["section"];
type ConfigSectionEntryName<I extends number> = typeof CONFIGS[I]["name"];

// Allows all permutations of section and name: "a_1" | "a_2" | "b_1" | "b_2" :(
// I only want "a_1" | "b_2" 
type CompleteConfigName<I extends number> =
  `${ConfigSections<I>}_${ConfigSectionEntryName<I>}`; 

But in the type CompleteConfigName<I extends number> the I seems to allow any number, as the type resolves to "a_1" | "a_2" | "b_1" | "b_2". But I want to enforce a specific index number I, so that the type results to "a_1" | "b_2"

CodePudding user response:

You should use a mapped type like this:

type CompleteConfigName = {
  [K in keyof typeof CONFIGS]: (typeof CONFIGS)[K] extends { 
    section: infer A, name: infer B
  }  
    ? `${A & string}_${B & string}` 
    : never
}[keyof typeof CONFIGS & `${bigint}`]

CompleteConfigName maps over each element in the tuple to create the string literal. We can index this type with [keyof typeof CONFIGS & '${bigint}'] to create a union of all elements inside the mapped type.

Playground

  • Related