说我有这种类型:

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.' },

等等,其中每个键值对的名称应为上述联合之一,并转换为小写。如何使它成为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>;

10-06 12:32