简体   繁体   English

如何在接口中定义字符串数组?

[英]How to define array of strings in interface?

I tried this: 我尝试了这个:

export interface ITarifFeatures {
  key: string;
}


export interface ITarif {
   features: ITarifFeatures[];
}

Then I have create object based on interface: 然后,我基于接口创建对象:

let obj<ITarif> {
      name: "Базовый",
      description: "Подходит для...",
      currency: "A",
      price: 1.99,
      period: "Месяц",
      features: ["СМС уведомления"]
    };

But this property is wrong: 但是这个属性是错误的:

features: ["СМС уведомления"]

Also I tried: 我也尝试过:

export type ITarifFeatures = {
  key: string[];
}

export interface ITarif {
  name: string;
  description: string;
  currency: string;
  price: number;
  period: string;
  features: ITarifFeatures
}

The interface type ITarifFeatures expects a property called key that you are not supplying, you are passing an instance of string type ["СМС уведомления"] in the array instead, so modify the code to this: interface类型ITarifFeatures需要一个您不提供的名为key的属性,而是在数组中传递string类型["СМС уведомления"]的实例,因此将代码修改为此:

export interface ITarifFeatures {
  key: string;
}
export interface ITarif {
    features: ITarifFeatures[];
    [x: string]: any 
}

let itarif: ITarifFeatures = {key: "СМС уведомления"};
let obj: ITarif = {
      name: "Базовый",
      description: "Подходит для...",
      currency: "A",
      price: 1.99,
      period: "Месяц",
      features: [itarif]
};

Also, the ITarif type will only accept features property, but you are trying to supply more key-values to it. 另外, ITarif类型将仅接受features属性,但是您尝试为其提供更多键值。 To circumvent it add an indexer [x: string]: any in the original interface. 为了避免这种情况,请在原始接口中添加一个索引器 [x: string]: any

String type != ITarifFeatures 字符串类型!= ITarifFeatures

What is need is an object like this: 需要的是这样的对象:

{
    key:'blabla'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM