I
I
Ilya2019-10-13 11:56:52
JavaScript
Ilya, 2019-10-13 11:56:52

How to set the structure of an object in the interface, with a mandatory key and any number of another type?

There is such a structure for writing commands:

export interface cmd {
    _about: string,
    id: number,
}
export interface category {
    _about: string,//Вот тут проблема
    [cmd: string]: cmd
}
export interface cmdList {
    [category: string]: category
}

const list: cmdList = {
    system: {
        _about: "Системные команды",
        ok: {
            _about: "Успешно принятая команда",
            id: 0
        },
        err: {
            _about: "Ошибка принятие команды",
            id: 1
        }
    }
};

How to correctly set the required field _about of type string in category, and all other fields of type cmd?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-10-13
@Mr_Epic

https://basarat.gitbooks.io/typescript/docs/types/... section "Excluding certain properties from the index signature"
In your case it will look like this .
It's better to construct this object differently:

{
  system: {
    _about: ...,
    commands: {
      ok: { ... },
      err: { ... }
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question