Я пытаюсь сделать что-то подобное в Typescript:
type A = 'stuff'
type B = {
[x: string]: {
active?: {
[name: string] : A
}
[name: string] : A
}
}
Но я получаю: Property 'active' of type '{ [name: string]: A; } | undefined' is not assignable to string index type 'A'.
Я понимаю, что active
также признается в качестве участника из [имя: строка]. Есть ли что-нибудь, что я могу сделать, чтобы точно указать, что active
может содержать что-то отличное от других клавиш?
Надеюсь, это не дубликат, мне было трудно найти хороший способ описать проблему в Google .
** Обновление: **
Следующий код не работает с Typescript
type ImageName = 'test'
type IconEntry = { [name: string]: ImageName }
type Icons = {
[namespace: string]: IconEntry & {
active?: IconEntry;
};
}
const icons: Icons = {
buttons: {
switch: 'test',
active: {
switch: 'test'
}
}
}
Type '{ switch: "test"; active: { switch: "test"; }; }' is not assignable to type 'IconEntry & { active?: IconEntry | undefined; }'.
Type '{ switch: "test"; active: { switch: "test"; }; }' is not assignable to type 'IconEntry'.
Property 'active' is incompatible with index signature.
Type '{ switch: "test"; }' is not assignable to type '"test"'.(2322)
input.ts(4, 3): The expected type comes from this index signature.