BK52
BK52

Reputation: 934

Node Js and Typescript call global function with parameters

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?

Upvotes: 0

Views: 42

Answers (1)

NG235
NG235

Reputation: 1271

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.

Upvotes: 1

Related Questions