Argument of type 'StoreState' is not assignable to parameter of type 'PartStoreState, layers'
Am I dumb ? I think so...
the error occur in setOne method declaration
/**
* Store type
*/
type StoreState = {
readonly layers: Layer[]
}
/**
* Context type
*/
type ContextState = [
state: StoreState,
actions: {
/**
* Set all layers
* @param layers
*/
setAll?: (layers: Layer[]) => Promise<void>
/**
* Set one layer
* @param layer
*/
setOne?: (layer: Layer) => Promise<void>
/**
* Remove a layer
* @param id - the id of the layer to remove
*/
remove?: (id: string) => Promise<void>
}
]
/**
* Initial state of the store
*/
const initialState = {
layers: []
}
/**
* Context
*/
const LayerContext = createContext<ContextState>([initialState, {}])
/**
* Provider
*/
export const LayerProvider: ParentComponent = (props) => {
const [state, setState] = createStore<StoreState>(initialState)
const setAll: ContextState[1]['setAll'] = async (layers) => {
setState('layers', layers)
}
const setOne: ContextState[1]['setOne'] = async (layer) => {
setState(
(state) => state.id === layer.id,
produce((layers) => layer)
)
console.error('Not Implemented')
}
const remove: ContextState[1]['remove'] = async () => {
setState('layers', [])
}
return (
<LayerContext.Provider value={[state, { setAll, setOne, remove }]}>
{props.children}
</LayerContext.Provider>
)
}
const useLayer = () => useContext(LayerContext)
export default useLayer
/**
* Store type
*/
type StoreState = {
readonly layers: Layer[]
}
/**
* Context type
*/
type ContextState = [
state: StoreState,
actions: {
/**
* Set all layers
* @param layers
*/
setAll?: (layers: Layer[]) => Promise<void>
/**
* Set one layer
* @param layer
*/
setOne?: (layer: Layer) => Promise<void>
/**
* Remove a layer
* @param id - the id of the layer to remove
*/
remove?: (id: string) => Promise<void>
}
]
/**
* Initial state of the store
*/
const initialState = {
layers: []
}
/**
* Context
*/
const LayerContext = createContext<ContextState>([initialState, {}])
/**
* Provider
*/
export const LayerProvider: ParentComponent = (props) => {
const [state, setState] = createStore<StoreState>(initialState)
const setAll: ContextState[1]['setAll'] = async (layers) => {
setState('layers', layers)
}
const setOne: ContextState[1]['setOne'] = async (layer) => {
setState(
(state) => state.id === layer.id,
produce((layers) => layer)
)
console.error('Not Implemented')
}
const remove: ContextState[1]['remove'] = async () => {
setState('layers', [])
}
return (
<LayerContext.Provider value={[state, { setAll, setOne, remove }]}>
{props.children}
</LayerContext.Provider>
)
}
const useLayer = () => useContext(LayerContext)
export default useLayer
3 Replies
hum so I fixed it like that can someone tell me if it's wrong ?
const setOne: ContextState[1]['setOne'] = async (layer) => {
setState(
'layers',
produce((layers) => {
const prevLayer = layers.find((l) => l.id === layer.id)
if (prevLayer) {
_.merge(prevLayer, layer)
} else {
layers.push(layer)
}
})
)
console.error('Not Implemented')
}
const setOne: ContextState[1]['setOne'] = async (layer) => {
setState(
'layers',
produce((layers) => {
const prevLayer = layers.find((l) => l.id === layer.id)
if (prevLayer) {
_.merge(prevLayer, layer)
} else {
layers.push(layer)
}
})
)
console.error('Not Implemented')
}
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
thanks a bunch !
The error handling for not initialized context is dope