1
常見的使用情況下,對JavaScript對象是使用它們作爲鍵值存儲...有點像一本字典:打字稿接口聲明:可轉位屬性
var dictionary = {},
value;
dictionary['key a'] = 99;
dictionary['key b'] = 12;
value = dictionary['key a']; // 99
打字稿智能感知善良可以通過聲明的加入接口這樣的:
interface IIndexable<T> {
[s: string]: T;
}
和使用這樣的接口:
var dictionary: IIndexable<number> = {},
value: number;
dictionary['key a'] = 99;
dictionary['key b'] = 'test'; // compiler error: "cannot convert string to number"
var x = dictionary['key a']; // intellisense: "x" is treated like a number instead of "any".
這裏是我的問題:是否有可能宣佈該接口的獨立版本:
interface StackOverflow {
questions: IIndexable<number>;
}
即不使用IIndexable?
我試圖做這樣的事情,但它不會編譯:
interface MyAttempt {
questions: [s: string]: number;
}