Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Index Signature of object type implicitly has type any

Tags:

typescript

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?

like image 280
Aj1 Avatar asked Oct 31 '22 16:10

Aj1


1 Answers

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;
    }
};
like image 59
David Sherret Avatar answered Nov 15 '22 06:11

David Sherret