有没有更简洁的方式来在 TypeScript 中表示这个 JSON 对象?

Posted

技术标签:

【中文标题】有没有更简洁的方式来在 TypeScript 中表示这个 JSON 对象?【英文标题】:Is there a more concise way to represent this JSON object in TypeScript? 【发布时间】:2020-10-14 23:57:57 【问题描述】:

我有一个从 JSON 对象生成的接口:

export interface ValidationMsg 
  stringMinCharsValidation: string;
  stringMaxCharsValidation: string;
  stringMaxCharsForValidation: string;
  stringMaxCharsValidationWithKey: string;
  stringMinCharsValidationWithKey: string;
  stringThreeChars: string;
  moreThanThreeChars: string;
  coverMoreThan5: string;
  coverLessThan100: string;
  coverLessThan45: string;
  firstNameThree: string;
  firstNameWrongFormat: string;
  firstNameNoSpace: string;
  lastNameWrongFormat: string;
  lastNameNoSpace: string;
  numberLessThanLowerBound: string;
  numberMoreThanUpperBound: string;
  numberLessThanLowerBoundKgs: string;
  numberMoreThanUpperBoundKgs: string;
  numberLessThanLowerBoundMtrs: string;
  numberMoreThanUpperBoundMtrs: string;
  notNull: string;
  yearNot4: string;
  onlyNumbers: string;
  htmlTagsNotAllowed: string;
  validDate: string;
  validDateFormat: string;
  pastDate: string;
  futureDate: string;
  birthdayMax: string;
  birthdayMin: string;
  validPhoneNumber: string;
  validEmail: string;
  emailMaximumLength: string;
  notUkResident: string;
  pleaseSelect: string;
  validPostCode: string;
  validSortCode: string;
  validAuthorisation: string;
  validAccountNumber: string;
  noNumbers: string;
  invalidBankDetails: string;
  unknownSortCode: string;
  invalidAccountNumber: string;
  notEmpty: string;
  otherTitleNotNull: string;
  notEqualUnknown: string;
  invalidPassword: string;
  noValue: string;
  passwordsDontMatch: string;
  postcodeIsNotGuernseyOrChannelIslands: string;
  pleaseConfirm: string;
  pleaseCheckOCIS: string;
  mandatoryAddressFields: MandatoryAddressFields;
  firmNameThree: string;
  firmNameWrongFormat: string;
  firmNameNoSpace: string;
  firmReferenceWrongFormat: string;
  firmReferenceNoSpace: string;
  wrongFormat: string;
  noSpace: string;
  noSpaceStart: string;
  groupNameWrongFormat: string;
  groupNameNoSpace: string;
  groupReferenceWrongFormat: string;
  groupReferenceNoSpace: string;
  isNotNull: string;
  isValidNumber: string;
  isGreaterThanLowerBound: string;
  isLessThanUpperBound: string;

export interface MandatoryAddressFields 
  AUS: string;
  GBR: string;
  DEFAULT: string;

我最初使用Rcord<string, string> 将其表示为接口,但由于MandatoryAddressFields 而不起作用。

有没有更简洁的表示方式?

【问题讨论】:

interface ValidationMsg mandatoryAddressFields: MandatoryAddressFields, [key: string]: any ? 谢谢,好多了 您要允许任何密钥还是需要保证这些确切的密钥? 任何键都可以 【参考方案1】:

一个不那么严格的方法可能是这样的:

interface ValidationMsg 
    mandatoryAddressFields: MandatoryAddressFields,
    [key: string]: any


interface MandatoryAddressFields 
  AUS: string;
  GBR: string;
  DEFAULT: string;

【讨论】:

以上是关于有没有更简洁的方式来在 TypeScript 中表示这个 JSON 对象?的主要内容,如果未能解决你的问题,请参考以下文章

怎样写出简洁的TypeScript项目

是否有更简洁的 linq 方式来“联合”单个项目?

Java中foreach添加String数组到List集合,有没有更简洁的方式?

如何在 TypeScript 中表示包含多种类型的二维数组?

typescript中表示同时满足两个或两个以上条件的定义,也就是&符号

Collectors.toMap() keyMapper——更简洁的表达方式?