简体   繁体   English

TypeScript:如何为具有许多相同类型的键和相同类型的值的对象创建接口?

[英]TypeScript: How to create an interface for an object with many keys of the same type and values of the same type?

I'm building a React Native app in TypeScript with Redux and Normalizr .我正在使用 Redux 和Normalizr在 TypeScript 中构建一个 React Native 应用程序。 So I will have noramlized state.所以我会有规范化的状态。

I have four interfaces: Emotion , Need , PainData and PainReport :我有四个接口: EmotionNeedPainDataPainReport

export interface Emotion {
  name: string;
  chosen: boolean;
  rating: number;
}

export interface Need {
  name: string;
  rating: number;
}

export interface PainData {
  note: string;
  emotions: Emotion[];
  needs: Need[];
  date: Date;
}

export interface PainReport {
  [date: string]: PainData
}

Now I would like to create an interface that is not an array, but an object an allows several PainReports like this (pseudo code):现在我想创建一个接口,它不是一个数组,而是一个允许多个这样的 PainReports 的对象(伪代码):

export interface PseudoPainReportsObject {
  [date: string]: PainData,
  [date: string]: PainData,
  [date: string]: PainData,
  // ... dynamically have as many as I'd like. Maybe 1, maybe 100
}

I want to use this for normalized state like you get when using Normalizr.我想将它用于规范化状态,就像使用 Normalizr 时一样。

How would one do such a type or interface?如何做这样的类型或接口?

One liner using TypeScript's Record type:使用 TypeScript 的Record类型的一个班轮:

type PseudoPainReportsObject = Record<string, PainData>;

Record<K, V> represents an object with any number of K type keys that map to V type values. Record<K, V>表示具有任意数量的K类型键的对象,这些键映射到V类型值。

[date: string] allows arbitrarily many properties; [date: string]允许任意多个属性; PainReport does exactly what you want. PainReport正是您想要的。

There is no way to constrain it to only one property.没有办法将它限制为只有一个属性。

暂无
暂无

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

相关问题 如何基于 TypeScript 中的 const object 的键/值创建 object 类型? - How to create an object type based on keys/values of a const object in TypeScript? 在 TypeScript 中将对象键/值映射到具有相同键但不同值的对象的强类型 - Strongly type a map of object keys/values to an object with the same keys but different values in TypeScript Typescript:使用相同的键但不同的值创建 object - Typescript: Create object with same keys but different values 如何创建一个对象类型,它具有动态数量的键,具体取决于同一对象中字符串数组的值? - How to create a type of an object, which has a dynamic number of keys, depending on the values of a string array, which is in the same object? 如何在打字稿中创建枚举值作为类型/接口? - How to create enum values as type/interface in typescript? 是否可以在 TypeScript 上创建一个 object 接口,允许灵活数量/类型的键? - Is it possible to create an object interface on TypeScript that allows a flexible number/type of keys? 是否可以从另一种类型的 object 中创建一种类型的 object,具有相同的键名,但它们的值类型不同? - Is it possible to create a type of object from another type of object with the same keys names, but different types of values of them? TypeScript:同一类型/接口内的条件类型 - TypeScript: conditional type inside the same type/interface 如何根据同一类型中提到的键在打字稿中声明类型? - How to declare a type in typescript based on the keys mentioned in the same type? 创建一个类型,其嵌套键与另一种类型相同,但所有值都是字符串 - Create a type with the same nested keys as another type but all values are string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM