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?

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;
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.