Typescript 类型脚本动态联合
我正在尝试创建一个类型“helper”,它将转换为一个联合体,最好通过以下代码段来表达:Typescript 类型脚本动态联合,typescript,Typescript,我正在尝试创建一个类型“helper”,它将转换为一个联合体,最好通过以下代码段来表达: type emailResponse = { email: string } NetworkActions<"EMAIL", emailResponse> // translating to a union {type: 'EMAIL_LOADING'} | {type: 'EMAIL_SUCCESS', payload: emailResponse} | {type:
type emailResponse = {
email: string
}
NetworkActions<"EMAIL", emailResponse>
// translating to a union
{type: 'EMAIL_LOADING'} | {type: 'EMAIL_SUCCESS', payload: emailResponse} | {type: 'EMAIL_ERROR'}
type emailResponse={
电子邮件:string
}
网络行动
//翻译成工会
{type:'EMAIL_LOADING'}}{type:'EMAIL_SUCCESS',负载:emailResponse}{type:'EMAIL_ERROR'}
第一个泛型需要用作联合的三个元素的前缀。第二个泛型需要传递给联合体的第二个元素
这可以用TypeScript实现吗?如何实现?虽然目前不可能,但在typescript 4.1发布后(2020年11月)很快就会实现
type emailResponse={
电子邮件:string
}
键入NetworkActions=
|{type:`${P}{u加载`}
|{type:`${P}{u错误`}
|{type:`${P}\u SUCCESS`,有效负载:T}
类型R=网络操作
与问题“”一样,将“电子邮件”
与“加载”
连接以生成“加载电子邮件”
的功能是
这一点可以通过以下方式公开获得
type NetworkActions note夜间操场版本4.1.0-dev.20201015
type emailResponse = {
email: string
}
type NetworkActions<P extends string, T> =
| { type: `${P}_LOADING` }
| { type: `${P}_ERROR` }
| { type: `${P}_SUCCESS`, payload: T }
type R = NetworkActions<"EMAIL", emailResponse>