S
SolidJS15mo ago
aryzing

Type of setStore args?

When trying to listen for any and all changes to a store value, seems the only way of being able to do so is to wrap the store's setter like so,
const [store, _setStore] = createStore({});
function setStore(...args) {
_setStore(...args);
doStuff(store);
}
const [store, _setStore] = createStore({});
function setStore(...args) {
_setStore(...args);
doStuff(store);
}
What is the correct type for ...args? When typed as below, the type of the args is never, which throws errors in the existing setters,
function setStore(...args: Parameters<typeof setState>) {
_setStore(...args);
doStuff(store);
}
function setStore(...args: Parameters<typeof setState>) {
_setStore(...args);
doStuff(store);
}
2 Replies
Alex Lohr
Alex Lohr15mo ago
Try to type the whole setter function with SetStoreFunction.
aryzing
aryzingOP15mo ago
Gave it a try, no luck unfortunately. Both of the following produce a TS error saying ...args is of type any[].
const setStore: typeof _setStore = (...args) => { // Rest parameter 'args' implicitly has an 'any[]' type. ts(7019)
_setStore(...args);
doStuff(store);
};

const setStore: SetStoreFunction<{id: string, title: string}> = (...args) => { // Rest parameter 'args' implicitly has an 'any[]' type. ts(7019)
_setStore(...args);
doStuff(store);
};
const setStore: typeof _setStore = (...args) => { // Rest parameter 'args' implicitly has an 'any[]' type. ts(7019)
_setStore(...args);
doStuff(store);
};

const setStore: SetStoreFunction<{id: string, title: string}> = (...args) => { // Rest parameter 'args' implicitly has an 'any[]' type. ts(7019)
_setStore(...args);
doStuff(store);
};
Thanks for the suggestion though!
Want results from more Discord servers?
Add your server