我想獲得一種用于 redux 狀態的型別,它將用于模擬所述狀態。
到目前為止,我發現我有這個 Reducer:
(alias) const reducer: {
selectedElement: Reducer<StructureElement, AnyAction>;
fetchedState: Reducer<FetchedState, AnyAction>;
... 10 more ...;
mesApi: Reducer<...>;
}
所以我想做的是從零件內部以某種方式獲取StructureElement,FetchedState等等Reducer</here/, AnyAction>;。
我怎樣才能做到這一點?
或者有沒有更好的方法來獲得組合狀態型別?雖然它必須自動完成。我不想在應該自動生成的 2 個地方更改型別。
如果我試試這個:
type State = typeof reducer;
我得到
輸入 '{ selectedElement: Reducer<StructureElement, AnyAction>; fetchedState: Reducer<FetchedState, AnyAction>; ……還有 10 個……;mesApi: 減速器<...>; }' 不能分配給型別 '{ selectedElement?: { stateId: string; parentId:字串;hasChild:布林值;路徑串列:字串[];名稱:字串;顏色:字串;isActive:布林值;級別:數量;孩子們?: ...[]; 鍵?:字串;型別:狀態型別;}; ……還有 11 個……;mesApi?: { ...; }; }'。屬性“selectedElement”的型別不兼容。
這是有道理的。
uj5u.com熱心網友回復:
我建議簡單地對其進行建模,然后在需要它們的地方匯入這些型別:
export type UserState = {
readonly isLoggedIn: boolean;
// other user state here
};
// Type for entire global redux state
export type AppState = {
users: UserState;
// other sub-states here
};
uj5u.com熱心網友回復:
我個人為我想要的狀態中的每個功能創建一個切片,然后我有一個檔案,我將首先在其中宣告和匯出我的 RootState 型別,然后我將組合所有的減速器。
export interface RootState {
pokemon: pokemonState;
pokemonTrainer: pokemonTrainerState;
}
const reducer = combineReducers<RootState>({
pokemon: pokemonSLice,
pokemonTrainer: pokemonTrainerSlice,
});
凡pokemonState和pokemonTrainerState是每個型別特征的。
然后我會RootState在任何需要的地方使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/372384.html
標籤:javascript 打字稿 还原 类型 反应还原
