【发布时间】:2020-06-21 11:25:44
【问题描述】:
我正在使用带有 Redux 的 TypeScript。此代码工作正常:
export type action =
| {
type: "do-other-thing";
name: string;
}
| {
type: "do-something";
index: number;
};
function reducer(state: state = initState, action: action): state {
switch (action.type) {
case "do-something":
if (action.index === 0) return state;
// More logic
case "do-other-thing":
// More logic
default:
return state;
}
但是我想将 switch 语句中的返回案例移动到它们自己的函数中:
function doSomething(state: state, action : action): state {
if (action.index === 0) return state;
// more logic
}
function reducer(state: state = initState, action: action): state {
switch (action.type) {
case "do-something":
return doSomething(state, action);
case "do-other-thing":
// More logic
default:
return state;
}
现在doSomething 函数出现 TypeScript 错误:
“操作”类型上不存在属性“索引”。属性“索引” 不存在于类型'{ type: "do-something";宽度:数字; isLandscape:布尔值; }'.ts(2339)
我可以看到 TypeScript 不知道由于 switch 语句,action 对象将具有 index 属性。有没有办法让它意识到这一点?
【问题讨论】:
-
Typescript 怎么可能知道?您在 switch 语句之外定义函数,Typescript 无法知道您使用该函数的唯一位置是在 case 块内。
-
我知道我们需要明确定义
action的类型,但为什么理想的 TypeScript 编译器可能不知道它何时只在一个地方使用?
标签: typescript redux