Having The Same Context Transfer Different Properties
im trying to make a base widget, where it has common stuff amongst the widget elements(canvas for now, there will be the reference panel as well)
note: BaseCanvasPropertiesInterface is a placeholder
on the context here i want to supply
BaseWidgetPropertiesInterface
however, i can't just supply it like so bc my other widgets will inherit from this interface and expand with their own properties the naive approach would be making another context but ideally i want to handle this case from this context28 Replies
here is the creator function code:
RenderableComponent
just contains element(the thing u see) and properties(the properties u can change for the element) and RenderableComponentProperties contains only the ID as a common parameter
so in other words
ideally i want to expose the nesscarry properties, not just BaseWidgetPropertiesInterface
in this one useBaseWidgetContext
without creating a lot of contextswanna delete the messages in #general now that you're here
ig so
there
i think you either have to have 1 context with a shared type like the above, or a bunch of type-specific contexts, since you can't have a generic context
so that means
if i have say
BaseCanvasPropertiesInterface
that inherits from BaseWidgetPropertiesInterface
i have to make a context for thatyou don't have to, you'd just lose a bit of typesafety. you could make a
useCanvasWidgetContext
that just calls useBaseWidgetContext
and overwrites the typeoh
but for the
BaseWidgetProvider
?if you want to keep the base and canvas properties in separate contexts then you'd need more than one, i think i was assuming you take the canvas proeprties and merge them with the base properties
tbh i could try to inherit from the
BaseWidgetPropertiesInterface
i think personally i wouldn't have the canvas properties inherit from the widget properties
and add my own params
ah ok
let the canvas-specific properties exist on their own, and have the widget properties in another area
then you can pull each of them where necessary
so i can't escape 2 contexts at once
unless you merge the properties objects no
ok
also unless you don't need to get the widget context in the canvas
tbh im thinking of making this a factory method
since this pattern is used commonly
since you could overwrite the outer canvas properties context with the base widget context
mhm
but idk how much sense that would make if the canvas properties inherit from the base properties in the first place
i mean i got it, its inevitable
for the above tho, is it reccomended if i do something like have
createContextBoilerplate<T>
yeah sure
to not have to write the same thing basically over and over again
though i think i'd use
createContextProvider
from solid primitivessubProperties
field
where it would also be a object
turn it maybe to something like this
i don't think tbh it would work out
and as u said its better to have the contexts seperate
i might have to embrace thisHave you considered implementing the context value as a
discriminated union
?
Right up front, it's not as convenient as polymorphism where you can arbitrarily add new morphs, as it means that you have to modify the union whenever you add a new kind of context value but I wonder if that could be mitigated by somehow injecting the union as a type parameter into the overall context.
The kinds of context values would be designed for consumer needs while "provider components" would simply chose the most appropriate kind of context to provide. As all members of the discriminated union would implement the core properties no further narrowing would be needed to access those. Those consumers who do need access to extended properties could use the value kind
to narrow the context value to gain access.
Remove subclass refactoring
In many ways it's accepting early that if you are going to be needing instanceof
(or type predicates) for type narrowing later anyway, just accept it and make it a discriminated union.
Though there is the difference that kinds are distinct, while interfaces can overlap (though you could add narrowing functions that are maintained together with the discriminated union).MDN Web Docs
instanceof - JavaScript | MDN
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. Its behavior can be customized with Symbol.hasInstance.
Documentation - Narrowing
Understand how TypeScript uses JavaScript knowledge to reduce the amount of type syntax in your projects.
like have a sort of type property that has the discriminated union of
canvas
and base-widget
?
i've tried that, tbh it feels wrong to me on many levels]The thing is if you are going to use just one type of context you'll need to use (runtime) type narrowing anyway to get access to any extended properties if they are present.
Also you are still thinking in terms of “kind of provider”. Context consumers really don't care about that. They care about “do you have the type of properties that I need?”