2017-10-07 80 views

回答

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 
相關問題