admin管理员组文章数量:1420073
I have a number of different email templates that each have different parameters.
I can define a type for each template:
type TemplateType = 'weleEmail' | 'referralEmail' | 'loginEmail'
And for each template I can define parameters:
interface WeleEmail {
firstName: string;
lastName: string;
...
}
I a can then define an EmailTemplate interface:
interface EmailTemplate {
template: TemplateType;
params: WeleEmail | ReferralEmail | LoginEmail;
}
Is there a way to type this so that when the template is 'weleEmail' the params
type is WeleEmail
?
I have a number of different email templates that each have different parameters.
I can define a type for each template:
type TemplateType = 'weleEmail' | 'referralEmail' | 'loginEmail'
And for each template I can define parameters:
interface WeleEmail {
firstName: string;
lastName: string;
...
}
I a can then define an EmailTemplate interface:
interface EmailTemplate {
template: TemplateType;
params: WeleEmail | ReferralEmail | LoginEmail;
}
Is there a way to type this so that when the template is 'weleEmail' the params
type is WeleEmail
?
2 Answers
Reset to default 3It can be done in recent versions of Typescript if you change your types around a bit. This works by sourcing the TemplateType
and the EmailTemplate
from the same EmailTemplates
type. Typescript knows that members of TemplateType
correspond to keys of EmailTemplates
, and it can use that relationship to find the value types that are associated with a given TemplateType
.
type EmailTemplates = {
weleEmail: {
firstName: string;
lastName: string;
};
referralEmail: {
referrer: string;
};
}
type TemplateType = keyof EmailTemplates;
type EmailTemplate<T extends TemplateType> = {
template: T;
params: EmailTemplates[T]
}
const myEmail: EmailTemplate<'referralEmail'> = {
template: 'referralEmail',
params: { referrer: 'test' },
}
There may be some way to infer the TemplateType instead of having to pass it in as a generic type parameter, but I'm not sure how.
type TemplateType = 'weleEmail' | 'referralEmail' | 'loginEmail'
interface WeleEmail {
firstName: string;
lastName: string;
// ...
}
interface WeleEmailTemplate {
template: 'weleEmail';
params: WeleEmail;
}
interface ReferralEmailTemplate {
// ...
}
interface LoginEmailTemplate {
// ...
}
type EmailTemplate = WeleEmailTemplate | ReferralEmailTemplate | LoginEmailTemplate
const myEmail: EmailTemplate = {
template: 'weleEmail',
params: {
// then type checking and hinting is here for WeleEmailTemplate
},
}
本文标签: javascriptHow to use Typescript to define strongly typed email template parametersStack Overflow
版权声明:本文标题:javascript - How to use Typescript to define strongly typed email template parameters? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745327714a2653669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论