【发布时间】:2025-11-21 20:20:05
【问题描述】:
所以我尝试使用useReducer 挂钩创建一个reducer,该挂钩使用一个名为Action 的接口,该接口的属性可以是string 或number:
type Actions = 'update_foo' | 'update_bar';
interface Action {
type: Actions;
value?: number | string;
}
我还为初始状态定义了一个接口,并为设置默认状态定义了一个const:
interface InitialState {
foo: number;
bar: string;
}
const defaultState: InitialState = {
foo: 1,
bar: 'bar'
}
然后是我的 reducer 函数:
const fooBarReducer: React.Reducer<InitialState, Action> = (state: InitialState, action: Action) => {
switch(action.type) {
case 'update_foo':
return { ...state, foo: action.value };
case 'update_bar':
return { ...state, bar: action.value };
default:
return defaultState;
}
}
我遇到的这个问题是 Typescript 似乎不喜欢联合类型 def 并抛出以下错误:
Type '(state: InitialState, action: Action) => { foo: string | number | undefined; bar: string | number; }' is not assignable to type 'Reducer<InitialState, Action>'.
Call signature return types '{ foo: string | number | undefined; bar: string | number; }' and 'InitialState' are incompatible. The types of 'foo' are incompatible between these types.
Type 'string | number | undefined' is not assignable to type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
我之前使用过联合类型,但没有使用 Reacts useReducer 钩子。这可以通过在Action 接口中为foo 和bar 分别设置一个属性来解决,但如果可能的话,我想使用联合类型。
任何帮助将不胜感激!
【问题讨论】:
标签: reactjs typescript