好的...这是我的最后选择...
我尝试从“打字稿集”(https://github.com/basarat/typescript-collections/blob/release/src/lib/index.ts)扩充“词典”
typescript-collections
像这样导出字典:
export { default as Dictionary } from './Dictionary';
我的增强版应具有“排序”功能
这是我到目前为止所拥有的
src / augment.d.ts:
import { Dictionary } from 'typescript-collections';
declare module 'typescript-collections' {
export interface Dictionary<K, V> {
sort(): Dictionary<K, V>;
}
}
src / main / extensions / ts-collection.ts:
import { Dictionary } from 'typescript-collections';
// TS (WebStorm) shows my an error on "sort"
// Property "sort" does not exist on type "Dictionary<K, V>"...
Dictionary.prototype.sort = function<K, V>(): Dictionary<K, V> {
const sortedDict = new Dictionary<K, V>();
this.keys()
.sort()
.forEach((key) => {
const value = this.getValue(key);
if (value) {
sortedDict.setValue(key, value);
}
});
return sortedDict;
};
augment.d.ts包含在tsconfig.json中
mytest.spec.ts:(开玩笑)
import { Dictionary } from 'typescript-collections';
test('sort via prototype', () => {
let dict = new Dictionary<string, string>();
dict.setValue('a', 'a1');
dict.setValue('c', 'c1');
dict.setValue('b', 'b1');
expect(dict.keys()).toIncludeAllMembers(['a', 'b', 'c']);
expect(dict.keys().sort()[1]).toBe('b');
// Error: Property "sort" does not exist on...
dict = dict.sort();
expect(dict.keys()[0]).toBe('a');
expect(dict.keys()[1]).toBe('b');
expect(dict.keys()[2]).toBe('c');
});
[更新]
我的问题是,WebStorm向我显示了ts-collection.ts中的错误(请参阅代码注释),并且无法在mytest.spec.ts中(也包括代码注释)使用sort