I
I
Ilya2019-10-13 13:04:48
JavaScript
Ilya, 2019-10-13 13:04:48

How to set the type of the interface key in the interface?

There is an array of modules, how to limit the parent parameter to an array of keys of the same array (or another)?

export interface module {
    _about: string,
    parent?: modules.moduleID//Вот тут ошибка
}
export interface modules {
    [moduleID: number]: module
}

const list: modules = {
    0: {
        _about: "Root",
    },
    1: {
        _about: "Child",
        parent: 0 //Нужно ограничить до 0 | 1
    }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-10-13
@StockholmSyndrome

unfortunately this cannot be done, the type cannot know in any way what keys will be in an object of this type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question