6

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?

asked Oct 26, 2015 at 16:03

1 Answer 1

3

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;
 }
};
answered Oct 26, 2015 at 17:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.