Within the provided code snippet, if the 'as' keyword is omitted in each action, the inferred type for method widens to any of the Kind types. Is there a way to prevent having to repeat 'Kind.PAYPAL as Kind.PAYPAL'?
enum Kind {
CASH = 'CASH',
PAYPAL = 'PAYPAL',
CREDIT = 'CREDIT'
}
const Cash = () => ({
kind: Kind.CASH as Kind.CASH,
});
const PayPal = (email: string) => ({
kind: Kind.PAYPAL as Kind.PAYPAL,
email
});
const CreditCard = (payload: { cardNumber: string, cvv: string }) => ({
kind: Kind.CREDIT as Kind.CREDIT,
payload
});
type PaymentMethod = ReturnType<
typeof Cash
| typeof PayPal
| typeof CreditCard
>;
function describePaymentMethod(method: PaymentMethod): string {
switch (method.kind) {
case Kind.CASH:
// Here, method represents type Cash
return "Cash";
case Kind.PAYPAL:
// Here, method signifies type PayPal
return `PayPal (${method.email})`;
case Kind.CREDIT:
// Here, method denotes type CreditCard
return `Credit card (${method.payload.cardNumber})`;
}
}