Home > Mobile >  Node Js and Typescript call global function with parameters
Node Js and Typescript call global function with parameters

Time:07-20

I have a logger function in logger.ts

export const Logger=(message:string, module?:string, method?:string)=>{
    console.log(`${module} ${message}`)
}

In types/global.d.ts

declare global {
    var Log: any;
}
export {}

In index.ts

import { Logger } from './src/utils/logger';
global.Log=Logger;
global.Log("Hello world");

It works without any problem. But in global.Log i can't see the parameters and intellisense not working.

How can I see parameters for global.Log?

CodePudding user response:

I believe Intellisense is not working because the variable Log is of any type. Try defining global as below:

declare global {
    var Log: (message: string, module?: string, method?: string) => void;
}
export {};

Intellisense will need a specific type (not any or unknown) in order to suggest parameters.

Also, there is no need to prefix Log with global (i.e. global.Log) as Log is in the global context (accessible from anywhere), and it is not possible to use global.Log - global is not a variable.

  • Related