我很难理解这个问题:
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 }
我尝试了4种情况,但仍然不明白发生了什么。有人能解释一下为什么只有[index: string]: string;
会出现错误TS2411吗?
另一个例子:
codio@compact-guide
。我不认为有任何理由隐藏它。 - basarat