Skip to content
Advertisement

Vscode available properties intellisense using javascript or typescript in a function which’s parameter is a string

"screenshot of vscode"

Look here.

For the first parameter of the addEventListener function, vscode gives me several inbuilt suggestions

How should I make this available in my javascript function?

It could be using Jsdoc or typescript etc.

Advertisement

Answer

You can always see what vscode uses for intellisense by hovering on the addEventListener method or ctrl + click to go to the definition in lib.dom.d.ts file where all definitions are present.

By doing the above you will see that the vscode uses the keys of a class called WindowEventMap.

So your function accepting a event listener name could be

function myFunc(event: keyof WindowEventMap): void {
}

A .d.ts file contains typings for java script code. This is how you get typings like the one in your question and from some packages published to npm.

How is your javascript function used by others?

  1. Are you creating a javascript library which will be used by others? Then either use typescript during development and let typescript generate .d.ts files for your code for you. Or If you are not using typescript for development you will have to create your own declarations. And publish your library with these .d.ts file(s). Refer to DefenitelyTyped project for examples.
  2. A file in the same project which uses this function? If you are using typescript like i mentioned in the code snippet above, then intellisense will automatically be shown for users. Else if you are using javascript, then you can still try and create .d.ts files and refer to them in your ts config (Haven’t tried this myself).
Advertisement