简体   繁体   English

如何在打字稿中使用不同类型的通用键键入对象

[英]How to type an object with generic keys of different types in typescript

How would you type this objects in typescript ?你会如何在打字稿中输入这个对象?

I have one special "datetime" key that is a Date, the rest of the keys are numbers.我有一个特殊的“日期时间”键是日期,其余的键是数字。 But I don't know in advance which keys will be set on each object.但我事先不知道将在每个对象上设置哪些键。 Examples values:示例值:

type Metrics = ??????

const example1: Metrics = {
  datetime: new Date(),
  activity: 12.34,
  min: 12.34,
  max: 12.34,
}
const example2: Metrics = {
  datetime: new Date(),
  avg: 12.34,
}

Here is what I tried:这是我尝试过的:

type Metrics = {
  datetime: Date,
  [key: string]: number,
}
// ERROR on the type definition:
// Property 'datetime' of type 'Date' is not assignable to string index type 'number'


type Metrics = { 
    datetime: Date 
} & {  
    [key: string]: number 
}
// ERROR on the variable assignment:
// Type '{ datetime: Date; activity: number; min: number; max: number; }' is not assignable to type 'Metrics'.
//   Type '{ datetime: Date; activity: number; min: number; max: number; }' is not assignable to type '{ [key: string]: number | null; }'.
//     Property 'datetime' is incompatible with index signature.
//       Type 'Date' is not assignable to type 'number'.

You will want something that uses Record<Exclude<K,"datetime">, number> to map all present keys to type number but exclude the key "datetime".您将需要使用Record<Exclude<K,"datetime">, number>将所有现有键映射到类型 number 但排除键“datetime”的东西。 Since this won't necessarily be possible with a usual index type you are probably best off using a helper function that constrains the type as a generic:由于这对于通常的索引类型不一定可行,因此您最好使用将类型限制为泛型的辅助函数:

type Metrics<K extends keyof any> = { datetime: Date } & Record<Exclude<K, "datetime">, number>;

function makeMetrics<T extends Metrics<keyof T>>(metrics: T): T {
    return metrics;
}

const example1 = makeMetrics({
    datetime: new Date(),
    activity: 12.34,
    min: 12.34,
    max: 12.34,
});
const example2 = makeMetrics({
    datetime: new Date(),
    avg: 12.34,
});

This means that both examples will only allow the keys that are present when it is defined, adding extra keys later would not, in order to allow that you'd need something like "all keys except datetime are numbers" which is not possible as far as I know.这意味着两个示例都只允许定义时存在的键,稍后添加额外的键将不会,以便您需要诸如“除datetime外的所有键都是数字”之类的东西,这在目前是不可能的我所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript接口类型,可选不同类型的按键 - Typescript interface type with optional keys of different types 在Typescript中,如何将通用容器类型的对象合并到对象类型的通用容器中? - In Typescript, How can I merge object of generic container types into generic container of object type? 打字稿将不同泛型类型的数组合并为相同的泛型类型 - Typescript merge array of different generic types into the same generic type 如何在TypeScript的通用约束中表达“所有现有键下的值的类型都是某种类型”? - How to express “the types of the values under all existing keys are of some type” in generic constraints in TypeScript? 打字稿中具有通用键的对象 - Object with generic keys in typescript 如何为具有通用键的字典定义 typescript 类型? - How to define a typescript type for a dictionary with generic keys? 如何获取泛型类型(TypeScript)的键列表? - How to get a list of keys of generic type (TypeScript)? 如何在TypeScript中按通用类型过滤类型列表? - How to filter a list of types by a generic type in TypeScript? 如何在 typescript 中使用泛型类型和联合类型 - How to use generic type and union types in typescript 如何在 TypeScript 中迭代通用 object 的键? - How to iterate over keys of a generic object in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM