说我有这种类型:
export type VerificationStatus =
| 'TRIAL_DEPOSIT_PENDING'
| 'TRIAL_DEPOSIT_REQUIRED'
| 'TRIAL_DEPOSIT_EXPIRED'
| 'TRIAL_DEPOSIT_VERIFICATION_FAILED'
| 'TRIAL_DEPOSIT_REINITIATE'
| 'VERIFIED';
我有一个哈希映射,看起来可能像这样:
verificationStatus: { trial_deposit_expired: 'Sorry, trial deposits have expired.' },
etc,其中每个键值对的名称应为上面的并集之一,并转换为小写。如何将其设置为VerificationMapKey
类型,以便我可以做:const verificationStatus: {[name: VerificationMapKey]: string}
?
换句话说,VerificationMapKey
是一个函数的返回类型,该函数对VerificationStatus的所有可能值进行迭代并将其变为小写。
答案 0 :(得分:-1)
简单为:
const VerificationStatuses = ['TRIAL_DEPOSIT_PENDING',
'TRIAL_DEPOSIT_REQUIRED',
'TRIAL_DEPOSIT_EXPIRED',
'TRIAL_DEPOSIT_VERIFICATION_FAILED',
'TRIAL_DEPOSIT_REINITIATE',
'VERIFIED'];
const VerificationStausMessagesKey = VerificationStatuses.forEach(status => status.toLowerCase());
export type VerificationStatus = $Keys<typeof VerificationStatuses>;
export type VerificationStausMessageKey = $Keys<typeof VerificationStausMessagesKey>;