How to create a wrapper for SolidJS's setStore in TypeScript?
I have a SolidJS store defined:
I need to run some code before every execution of setStore, so I would like to wrap it with my own function. Is there a way to do that, that preserves all types and all overloads?
So far, the best solution I've (heard)[https://stackoverflow.com/questions/78209469/how-to-create-a-wrapper-for-solidjss-setstore-in-typescript?noredirect=1#comment137881170_78209469] is:
But the error suppression makes it ugly. Is there any clean way to do this?
Stack Overflow
How to create a wrapper for SolidJS's setStore in TypeScript?
I have a SolidJS store defined:
const [store, setStore] = createStore<SomeComplexType>();
I need to run some code before every execution of setStore, so I would like to wrap it with my own
5 Replies
You can directly get type of anything in ts with
typeof
. If you define your wrapper with same type as setStore
is, then the arguments and return value get automatically typed
That does not work, because of type inference not being able to figure out if the generic is the same in both cases.
I've tried for my storage primitive makePersisted(signalOrStore).
it gives an error:
A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)
Oh so that's weird. Also I see that it's fine with
But it's not what we want.
And I always treated spread operator as shorthand for this on variable number of arguments. I guess ts won't cover this case. For now if you just don't want these errors without
// @ts-expect-error
, you can also do
I think it's fine to use as any
internally in function as long as you know what you are doing and with properly set arguments / return value types, which typeof setStore
takes care of.that's better than
@ts-expect-error
. Thank you!