How do I destructure an object that extends multiple interfaces into each of those interfaces?
I'm looking for a way to extract the props, for each interface, from an object that extends multiple interfaces.
The use case for this is where I've created a composite component and want to expose all the properties from components A & B on C.
I appreciate
{ ...aProps, ...bProps }: C
is not valid, but is merely to simulate the desired outcome.6 Replies
I'll also add that, for small interfaces like those examples you could get away with
{ a1, a2, b1, b2 }
, but many interfaces come with a lot more properites than that <:mace_laughing:1176448783480012840> .I believe it's just going to come down to the function itself and it's return type.
(I'm presuming that the arguments are not being fed in as 2 separate args?)
No, I was trying to avoid doing:
I'm coming from a C# background where you can cast an object to it's interface (
var a = (A)cObject
) and you'd only get the properties that come from the interface A
at runtime.
C# doesn't have the notion of "prop spreading" though and I know at runtime the underlying object will have all the properties and spread both A
& B
props onto ComponentA
and ComponentB
.You can Pick(arguments, key of A)
It's important to bear in mind that unlike C++ interfaces, TypeScript is (in general) removed at build time. It has no effect on how the program runs.
Yup, understand that <:mace_laughing:1176448783480012840>
Seems like there should be something that works at runtime to split up an object