gpt4 book ai didi

javascript - 流 : transforming union type of multiple strings into new type

转载 作者:行者123 更新时间:2023-11-30 06:50:45 24 4
gpt4 key购买 nike

假设我有这种类型:

导出类型 VerificationStatus =
| 'TRIAL_DEPOSIT_PENDING'
| 'TRIAL_DEPOSIT_REQUIRED'
| '试用_存款_过期'
| 'TRIAL_DEPOSIT_VERIFICATION_FAILED'
| 'TRIAL_DEPOSIT_REINITIATE'
| '已验证';

我有一个散列映射,可能看起来像:

verificationStatus: { trial_deposit_expired: '抱歉,试用存款已过期。' },

等,其中每个键值对的名称应该是上面的联合之一,转换为小写。如何将它变成 VerificationMapKey 类型,这样我就可以做:const verificationStatus: {[name: VerificationMapKey]: string}?

换句话说,VerificationMapKey 是一个函数的返回类型,它遍历 VerificationStatus 的所有可能值并将它们变成小写。

最佳答案

简单如:

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>;

关于javascript - 流 : transforming union type of multiple strings into new type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com