网站搜索

typescript - 如何为 createEntityAdapter 对象的 getInitialState 的合并对象中的属性添加类型?


我想用 FetchingStatus 类型来输入状态。该如何实现?

type FetchingStatus = 'idle' | 'loading' | 'succeeded' | 'failed';

const initialState = companyPagesAdapter.getInitialState({
        status: 'idle',
        perPage: 1,
        lastPage: 1
    });

这是 getInitialState 的签名:

getInitialState<S extends object>(state: S): EntityState<T> & S

答案#1

type FetchingStatus = "idle" | "loading" | "succeeded" | "failed";
    
    
interface ExtendedEntityAdapterState {
    status: FetchingStatus
    perPage: number,
    lastPage: number
}

const initialState: ExtendedEntityAdapterState   = {
  status: 'idle',
  perPage: 1,
  lastPage: 1
});

companyPagesAdapter.getInitialState(initialState)

像这样的东西对我有效。Reducer 动作提供了正确的约束。