i get this error whenever i try to add a function to the global nodejs global namsepace in a TypeScript environment.
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature
declaring the global namespace
declare global {
namespace NodeJS {
interface Global {
signin(): string[]
}
}
}
so if i try this
global.signin = () => {}
it returns a
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature
You should declare a global declared as interface in global.d.ts like this:
export interface global {}
declare global {
var signin: ()=>string[]
}
Below is an example for Node v16
// globals.d.ts
declare module globalThis {
var signin: () => string[];
}
I too had the same issue. Fixed it by the below code.
declare global {
function signin(): Promise<string[]>
}
just declare variable by var keyword. like this:
// eslint-disable-next-line no-var
var hello = () => { console.log("hello world"); };
// eslint-disable-next-line no-var
var hi: () => number;
globalThis.hello();
globalThis.hi = () => 143;
You can declare variables in .d.ts files, too.
I was having a similar issue and I found that node's global typings were changed recently-ish; you can now override them by doing:
// global.d.ts
declare global {
function someFunction(): string;
var someVariable: string;
}
Note: this will not work with let or const you must use var.
// index.ts
global.someFunction = () => "some value";
global.someVariable = "some value";
You have to use the var keyword in declare global, and remove namespace NodeJS {.
Like this:
//globals.d.ts
import type { EventEmitter } from "events";
declare global {
var myGlobal: EventEmitter;
}
/// <reference path="globals.d.ts" />
//index.ts
// reference needs to be at top of file
import { EventEmitter } from "events";
global.myGlobal = new EventEmitter();
global.myGlobal // EventEmitter type: EventEmitter
window.myGlobal // EventEmitter type: EventEmitter
Or if you don't have any imports in the .d.ts file:
//namedoesntmatter.d.ts
declare var a: string;
//index.ts
global.a = "Hello"
global.a //Hello type: string
window.a //Hello type: string
in my own case i didn't quite realize until later that the global namespace i declared was case sensitive.
instead of this. before my question was edited it was namespace NODEJS
declare global {
namespace NODEJS {
interface Global {
signin(): string[]
}
}
}
it was supposed to be this
declare global {
namespace NodeJS {
interface Global {
signin(): string[]
}
}
}
pay attention to NODEJS and NodeJS. After i made these changes, typescript was cool with it and it work the way i expected it to.