A
A
asmodeus13th2021-08-17 14:27:09
typescript
asmodeus13th, 2021-08-17 14:27:09

How to write an interface for an array with objects?

I have a variable activatedPackages , which contains an array containing objects containing key-value pairs (mandatory and optional fields), where the value is always a string:

const activatedPackages = [
  {
    // обязательные поля
    id: 123, // число
    activated: true, // булевое
    name: '30 days', // строка
    // необязательные поля (несколько)
    type: 'blablabla' // any
  },
  {
    // обязательные поля
    id: 123, // число
    activated: true, // булевое
    name: '30 days', // строка
    // необязательные поля
    type: 'blablabla' // any
  },
];

The number of objects inside the array can be different. We need to write an interface for the activatedPackages variable .

PS I know, I want a lot, but I just can't figure out how to write such an interface. If you need to do several interfaces, then ok. The main thing is that it works)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alice, 2021-08-17
@asmodeus13th

interface IActivatedPackages {
    id: number;
    activated: boolean;
    name: string;
    // необязательные поля (несколько)
    n1?: string;
    n2?: string;
    n3?: string;
    n4?: string;
    // warn
    type: any;
}

Um.. on ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question