【发布时间】:2019-03-17 00:32:59
【问题描述】:
我正在使用 Redux 和 Normalizr 在 TypeScript 中构建一个 React Native 应用程序。所以我会有规范化的状态。
我有四个接口:Emotion、Need、PainData和PainReport:
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
}
现在我想创建一个不是数组的接口,而是一个对象,它允许像这样的多个 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
}
我想将它用于标准化状态,就像您在使用 Normalizr 时一样。
如何做这样的类型或接口?
【问题讨论】:
-
[date: string]允许任意多个属性。多次这样做是没有意义的。 -
听起来您现有的
PainReport界面已经完全符合您的要求。 -
@SLaks 你说得对,哇,谢谢。我猜我的问题是,如何设计一个不允许这样做的交互?那么只有一个键的界面?
-
没有办法。
-
@J.Hesters,这是一个老问题,但对于未来的读者来说,有一种方法可以将对象限制为一个键:stackoverflow.com/a/60807986
标签: typescript redux typescript-typings normalizr typescript-types