【发布时间】:2021-08-05 23:45:29
【问题描述】:
interface AppState {
jwt: string | null,
isSignedIn: boolean
isLoading : boolean;
newUser : boolean;
avatar : Avatar | null;
alerts : SnackBarAlert[];
}
const initialState : AppState = { isLoading : true, isSignedIn : false, newUser : false, hero : null, alerts : [], jwt : null };
const store = createContext(initialState);
const { state, dispatch } = useContext(store);
当我将鼠标悬停在状态或调度上时,我得到“属性 '状态' 不存在于类型 'AppState' 上”。和“'AppState' 类型上不存在属性 'dispatch'。”
我心想'哦,我需要告诉 useContext 商店是什么类型。但是当我试图描述这家商店时......
interface Store {
state: AppState
dispatch: <Payload = {}>(action: Action<Payload>) => void
}
const { state, dispatch } = useContext<Store>(store);
'store' 突出显示告诉我: “‘上下文’类型的参数不能分配给‘上下文’类型的参数。 'Provider.propTypes.value' 的类型在这些类型之间不兼容。 类型“验证器”不可分配给类型“验证器”。 类型 'AppState' 不可分配给类型 'Store'。”
我在这里做错了什么?
【问题讨论】:
标签: reactjs typescript