POV
POV

Reputation: 12005

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
}

Upvotes: 1

Views: 58

Answers (2)

Fullstack Guy
Fullstack Guy

Reputation: 16908

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:

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. To circumvent it add an indexer [x: string]: any in the original interface.

Upvotes: 1

FrV
FrV

Reputation: 258

String type != ITarifFeatures

What is need is an object like this:

{
    key:'blabla'
}

Upvotes: 1

Related Questions