尝试将命名对象关联到IBindings接口的空数组时,会出现此错误,但可以正常编译并且可以正常工作。
节点10.16.0,打字稿3.5.1,VSCode。 已经尝试更改为扩展到Array,但不起作用。
import { IBindings } from './ioc.interface';
export class ioc {
private _bindings: IBindings[];
constructor () {
this._bindings = [];
}
bind (namespace: string, closure: Function) {
if (typeof (closure) !== 'function') {
throw Error('IoC.bind expects second parameter to be a closure: ' + closure);
}
console.log(closure);
// Error is Right here
this._bindings[namespace] = {
closure: closure,
singleton: false,
cachedValue: null
}
console.log(`Binding ${namespace} to ioc container`);
}
}
export interface IBindings {
[namespace: string]: {
closure: Function,
singleton: boolean,
cachedValue: Function | null
}
}
我希望不会出现任何错误,因为它显然是需要传递给IBindings的字符串,但是输出是Element隐式具有“ any”类型,因为索引表达式不是“ number”类型。
答案 0 :(得分:0)
IBindings不是数组而是JS对象。所以课程的前三行应该是
private _bindings: IBindings;
constructor () {
this._bindings = {};
}