How to infer/map types dynamically from a given array of objects?

I have an array of objects something like this:

const options = [
  { name: "diameter", type: "number", value: 10, min: 0, max: 10 },
  { name: "height", type: "number", value: 2, min: 0, max: 8 },
  { name: "thickness", type: "number", value: 2, min: 0, max: 8 },
  { name: "text", type: "string", value: "Hello World" },
];

and I’d like to dynamically get the types, so have something like this;

type OptionsType = {
  diameter: { value: number; min: number; max: number };
  height: { value: number; min: number; max: number };
  thickness: { value: number; min: number; max: number };
  text: { value: string };
};

I’ve tried searching around, asking chatGPT / perplexity and supermaven but failed to have a solution.

Closes solution attempt from AI was this:

// Helper type to determine the correct parameter type based on 'type' property
type ParamType<T extends { type: string }> = T["type"] extends "number"
  ? { value: number; min: number; max: number }
  : { value: string };

// Mapped type to transform the options array into the desired type definition
type OptionsParams<T extends readonly { name: string; type: string }[]> = {
  [K in T[number] as K["name"]]: ParamType<Extract<T[number], { name: K }>>;
};

// Generate the type dynamically
type optionsParams = OptionsParams<typeof options>;

but on this return type of optionsParams is:

type optionsParams = {
    [x: string]: {
        value: number;
        min: number;
        max: number;
    };
}

so it’s only works (kind-of) just for values that are numbers.

Any suggestions how to approach this, and how to generate/map types dynamically from a given object array in TypeScript?

2

If you can assert name as const, this would work:

It omits name and type from the record value types, but the key is the const assertion of name.

const options = [
  { name: "diameter" as const, type: "number", value: 10, min: 0, max: 10 },
  { name: "height" as const, type: "number", value: 2, min: 0, max: 8 },
  { name: "thickness" as const, type: "number", value: 2, min: 0, max: 8 },
  { name: "text" as const, type: "string", value: "Hello World" },
];

// Mapped type to transform the options array into the desired type definition
type OptionsParams<T extends readonly { name: string; type: string }[]> = Partial<{
  [K in T[number] as K["name"]]: Omit<K, "name" | "type">;
}>;

// Generate the type dynamically
type optionsParams = OptionsParams<typeof options>;

Playground

1

TypeScript doesn’t infer object property value types as literals by default — so when you initialize the options variable without a type annotation or some kind of assertion (e.g. a type assertion or const assertion), the types of values at the "name" property in the objects are inferred simply as string. It is at this point that the type information that’s required for the desired mapped type is already lost, so more narrow inference is needed.

In order to capture those string literal types that are lost by default inference, you can use the array literal as the argument in a function that will create the object, and instruct the compiler to use literal inference for the "name" properties of each object using a const generic type parameter, like this:

type MappedOptions<T extends ReadonlyArray<Record<"name", string>>> = {
  [O in T[number] as O["name"]]: Omit<O, "name">;
};

// You can provide your own implementation for this function:
declare function toObject<
  const Name extends string,
  T extends ReadonlyArray<Record<"name", Name>>,
>(array: T): MappedOptions<T>;

Here’s a complete example:

TS Playground

type MappedOptions<T extends ReadonlyArray<Record<"name", string>>> = {
  [O in T[number] as O["name"]]: Omit<O, "name">;
};

// You can provide your own implementation for this function:
declare function toObject<
  const Name extends string,
  T extends ReadonlyArray<Record<"name", Name>>,
>(array: T): MappedOptions<T>;

const options = [
  { name: "diameter", type: "number", value: 10, min: 0, max: 10 },
  { name: "height", type: "number", value: 2, min: 0, max: 8 },
  { name: "thickness", type: "number", value: 2, min: 0, max: 8 },
  { name: "text", type: "string", value: "Hello World" },
];

// String literal types for "name" property already widened to "string"
// by default compiler inference (the type information is alrady lost)…
type InferredOptions = typeof options;
/*   ^? type InferredOptions = ({
  name: string;
  type: string;
  value: number;
  min: number;
  max: number;
} | {
  name: string;
  type: string;
  value: string;
  min?: never;
  max?: never;
})[] */

// …so it can't be used to get the desired mapped result:
const obj0 = toObject(options);
/*    ^? const obj0: {
  [x: string]: {
    type: string;
    value: number;
    min: number;
    max: number;
  } | {
    type: string;
    value: string;
    min?: never;
    max?: never;
  };
} */

// In contrast: using an object literal as the function argument
// allows for narrower inference:
const obj1 = toObject([
  { name: "diameter", type: "number", value: 10, min: 0, max: 10 },
  { name: "height", type: "number", value: 2, min: 0, max: 8 },
  { name: "thickness", type: "number", value: 2, min: 0, max: 8 },
  { name: "text", type: "string", value: "Hello World" },
]);

type Actual = typeof obj1;
/*   ^? type Actual = {
  diameter: {
    type: string;
    value: number;
    min: number;
    max: number;
  };
  height: {
    type: string;
    value: number;
    min: number;
    max: number;
  };
  thickness: {
    type: string;
    value: number;
    min: number;
    max: number;
  };
  text: {
    type: string;
    value: string;
    min?: never;
    max?: never;
  };
} */

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật