2017-02-03 57 views
0

不應該這樣做嗎?實現字典式的類

interface IDictionary { 
    [index: string]: string; 
} 

class Dictionary implements IDictionary { 
    public foo = "bar"; 
} 

相反,我發現了錯誤:

TS2420:Class 'Dictionary' incorrectly implements 'IDictionary'. Index signature is missing in type 'Dictionary'. 

回答

0

,因爲你實現一個接口

interface IDictionary { 
    [index: string]: string; 
} 

class Dictionary implements IDictionary { 
    public foo = "bar"; 
} 

你需要提供所有要求

interface IDictionary { 
    [index: string]: string; 
} 

class Dictionary implements IDictionary { 
    public foo = "bar"; 
    [index: string]: string; 
}