I am trying to attach property on window object. Here is my code for that.
cbid:string ='someValue';
window[cbid] = (meta: any) => {
tempThis.meta = meta;
window[cbid] = undefined;
var e = document.getElementById(cbid);
e.parentNode.removeChild(e);
if (meta.errorDetails) {
return;
}
};
Compiler starts throwing the following error.
TypeScript Index Signature of object type implicitly has type any
Can someone tell me where I am doing the mistake?
A quick fix would be to allow anything to be assigned to the window object. You can do that by writing...
interface Window {
[propName: string]: any;
}
...somewhere in your code.
Or you could compile with --suppressImplicitAnyIndexErrors
to disable implicit any errors when assigning to an index on any object.
I wouldn't recommend either of these options though. Ideally it's best not to assign to window, but if you really want to then you should probably do everything on one property then define an index signature that matches what's being assigned to it:
// define it on Window
interface Window {
cbids: { [cbid: string]: (meta: any) => void; }
}
// initialize it somewhere
window.cbids = {};
// then when adding a property
// (note: hopefully cbid is scoped to maintain it's value within the function)
var cbid = 'someValue';
window.cbids[cbid] = (meta: any) => {
tempThis.meta = meta;
delete window.cbids[cbid]; // use delete here
var e = document.getElementById(cbid);
e.parentNode.removeChild(e);
if (meta.errorDetails) {
return;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With