我有困難想明白這一點:打字稿接口陣列類型錯誤TS2411
There are two types of supported index types: string and number. It is possible to support both types of index, with the restriction that the type returned from the numeric index must be a subtype of the type returned from the string index.
While index signatures are a powerful way to describe the array and 'dictionary' pattern, they also enforce that all properties match their return type. In this example, the property does not match the more general index, and the type-checker gives an error:
interface Dictionary { [index: string]: string; length: number; // error, the type of 'length' is not a subtype of the indexer }
來源:TypeScript Handbook's interface
我已經試過4例,但仍不能明白髮生了什麼。任何人都可以解釋爲什麼只有[index: string]: string;
將有錯誤TS2411?
另一種情況:
你錯過了一個'codio @ compact-guide'。我看不出有什麼理由把它反正隱藏起來 – basarat
想念兩個...... –
不錯。隱藏它使我想了解更多:) – basarat