【发布时间】:2016-05-18 10:21:34
【问题描述】:
我在声明 Redux Action Type 时确实遇到了一些问题。根据定义,Redux Action应该有type属性并且可以有一些其他的属性。
根据 TypeScript interfaces page in HandBook(参见“Excess Property Checks”)我可以这样做:
interface IReduxAction {
type: string;
[propName: string]: any;
}
而且似乎在某些情况下也有效(比如声明变量)
const action: IReduxAction = {
type: 'hello',
value: 'world'
};
但如果我试图声明正在使用该操作的函数:
function hello(state = {}, action: IReduxAction) {
return { type: action.type, text: action.text };
}
失败并显示消息“IReduxAction 类型上不存在属性 text”。
我做错了什么?通用动作类型如何声明?
TypeScript Playground 上的实时示例是 here
附:我检查了“类似”的问题,例如Why am I getting an error "Object literal may only specify known properties"?,但还没有找到解决方案……
附言显然它确实在最新版本的 TypeScript 中工作。
【问题讨论】:
标签: typescript