TypeScript: Define record where keys must be belong to union
2 March 2023 (Updated 2 March 2023)
type CompanyTypeCode = 'ltd' | 'plc' | 'sole-trader'
type EmailTemplates = {
[Code in CompanyTypeCode]?: InvitationEmails
}
// Because we made the key of EmailTemplates optional, we don't have to define every value of the CompanyTypeCode union as a key
const emailTemplates: EmailTemplates = {
ltd: LimitedCompanyEmail,
plc: PublicCompanyEmail,
}
Tagged:
TypeScript
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment