我一起使用这些工具:
- 打字稿
- 吞咽
- Gulp-注入
我正在尝试执行以下操作:
module My {
interface IGulpInjectable extends string { // << Problem here!
[gulp_inject: string] : string;
}
export class Cache {
private items: { [key: string] : IGulpInjectable };
constructor() {
this.items = {
"item1": { gulp_inject: "file1.html" },
"item2": { gulp_inject: "file2.html" }
}
}
getItem(key: string){
return this.items[key].trim();
}
}
}
用包含文件内容的字符串gulp-inject
替换。{ gulp_inject: "x.html" }
这就是我想要IGulpInjectable
扩展的原因string
:这样trim()
TypeScript 就能理解类似的方法。
但是,extends string
无效。也不是extends String
。至少,不是我当前的构造函数代码,我不想改变它。
如何告诉 TypeScript 我的界面具有所有方法string
?
脚注,我目前的解决方法:
getItem(key: string){
return (<any> this.items[key]).trim();
}
但这并不是很令人满意。