我有:
export interface AppStateType {
isOnline: boolean
}
const AppContext = createContext([{}, () => { }]);
const AppProvider = (props) => {
const [appState, setAppState] = useState<AppStateType>({
isOnline: true
})
return <AppContext.Provider value={[appState, setAppState]}>
{props.children}
</AppContext.Provider>
}
export { AppContext, AppProvider }
当我尝试使用它时:
const [appState, setAppState] = useContext<AppStateType>(AppContext)
我收到打字稿错误:
Argument of type 'Context<{}[]>' is not assignable to parameter of type 'Context<AppStateType>'.
The types of 'Provider.propTypes.value' are incompatible between these types.
Type 'Validator<{}[]>' is not assignable to type 'Validator<AppStateType>'.
Type '{}[]' is not assignable to type 'AppStateType'.