Within the code snippet below, a
StoreOp<"vanilla">
is intended to function on a Store
with "vanilla"
as a flag, along with other flags. Unfortunately, the current constraints are incorrect, restricting a StoreOp<"vanilla">
to only operate on a FlagStore<"vanilla">
instead of also allowing it to work on a FlagStore<"vanilla", "chocolate">
.
The calls to setVanilla
and setChocolate
are generating errors due to this mismatch. A successful resolution would eliminate these errors, as noted in the comments against the final four lines of code.
An example of the error message displayed is...
Argument of type 'Store<{ vanilla: boolean; chocolate: boolean; raspberry: boolean; }>' is not assignable to parameter of type 'FlagStore<"vanilla">'.
Types of property 'write' are incompatible.
I am seeking guidance on how to define constraints for the generic function type StoreOp
, and the corresponding factory function createSetterOp
, so that any Store containing the Operated
flags AMONG its Stored
flags is deemed a valid late-bound argument for the StoreOp
. Presently, the constraint
S extends FlagStore<Operated>
is set in the wrong direction, and I am struggling to determine how to specify it in the opposite direction - where S
includes Operated
and additional flags.
A StoreOp
should be able to rely on each of its Operated
flags having a respective boolean value in the store, without being concerned about the availability of other flags. In defining the generic function StoreOp
, Operated
should be assignable to
Stored</code, rather than vice versa. I anticipate the late-bound typing of the generic function against the actual store will ensure that an edit made by a <code>StoreOp
implementation aligns with the Store's type, triggering a compiler error if it fails to copy over the other (unknown) flags when updating the store.
My broader API requires inference from both the Stored
flags and the Operated
flags to dictate other typings. The crux lies in expanding the StoreOp
type effectively.
You can find the code below in this Typescript Playground