我希望所有属性都具有某种类型,但我希望显式声明的属性覆盖它:
interface Potato {
a: number
[all:string]: string
}
答案 0 :(得分:1)
您可以使用intersection types:
来实现这一目标type PotatoAll = { [all: string]: string };
type Potato = PotatoAll & { a: number };
let p = {} as Potato;
p['foo'] = 'foo';
p.a = 1;
p['a'] = 1;
p['foo'] = 1; //error
p['a'] = 'a'; //error
p.a = 'a'; //error