Replies: 1 comment
-
Great question! Thanks for posting it. Short answer: There is no strong reason for using types throughout the SDK and not interfaces. If there is a strong wish in the community to export the API objects as interfaces, we could rewrite that. No problem 😺. The decision to use only type aliases boils down to personal preference, and there are some good points to be made on predictable code and consistency. There is a lot of good material out there explaining this better than I can manage. I recommend watching ByteGrad and Matt Pocock for an in-depth analysis. There are several ways to approach this, but here are a few ideas that may (or may not! 😊) be useful: const requestPaymentDetails = await client.payment.info(token, reference);
if (!requestPaymentDetails.ok) {
// Handle error
Deno.exit(1);
}
const paymentDetails = requestPaymentDetails.data;
// If we are certain that the metadata is of type VendureMetadata, and
// we want a consistent type for paymentDetails with metadata of type VendureMetadata,
// we can use the `as` keyword to cast the metadata to that type.
const paymentDetailsWithMetadata = {
...paymentDetails,
metadata: paymentDetails.metadata as VendureMetadata,
};
// If we are not certian metadata will be of VendureMetadata type, we can use the
// `in` keyword to check if the metadata has a property that is only present in
// VendureMetadata. If it does, we can safely cast the metadata to VendureMetadata.
if (
paymentDetails.metadata &&
"orderCode" in paymentDetails.metadata &&
"channelToken" in paymentDetails.metadata
) {
const paymentDetailsWithMetadata = {
...paymentDetails,
metadata: paymentDetails.metadata as VendureMetadata,
};
}```
|
Beta Was this translation helpful? Give feedback.
-
Hi,
Just curious if there is a reasoning behind the SDK preferring to use type aliases instead of interfaces?
As this makes it harder to extend SDK types, so we end up declaring new types.
Instead of extending existing interfaces we end up with workarounds like these:
Beta Was this translation helpful? Give feedback.
All reactions