11

I'm working on a JavaScript transpiler that apart from other things will also replace certain functions and variables upon build.

For example the following file (./src/my-module.js):

defineModule("MyModule", function(exports) {
  return exports;
});

Will be copied and converted to (./build/my-module.js):

(function(global, factory) {
  "use strict";
  if (typeof exports !== "undefined" && typeof module !== "undefined") module.exports.MyModule = factory(exports.MyModule || {});
  else factory(global.MyModule = {});
})(this, function(exports) {
  return exports;
});

Some of these functions could also return a result. In that case I would like to be able to declare the types of the parameters and the result of the function without using require. Is it possible to have a global .d.ts definition in VSCode?

So far, all I've done is add the functions to the global variable of eslint so as to not have errors.

3
  • I am not sure if I understood correctly. Is this a Typescript project? If yes, what about adding the types to the paths config in tsconfig.json? Let me know if this is what you are looking for, so I will prepare an answer with details later. Commented Jul 21, 2019 at 14:06
  • So you are trying to declare the type of defineModule without defining the function (because it will only be inlined by the transpiler, there is no actual callable function anywhere)? Commented Jul 21, 2019 at 14:19
  • @saulotoledo, I'm working on a transpiler, which means that Im working on a project (completely besides the point if it is JavaScript or TypeScript) that will alter the contents of JavaScript files. It would be ideal to have the declarations in a global .d.ts file somwhere that I can reference from all my projects without the need of a require statement. Commented Jul 21, 2019 at 14:20

2 Answers 2

7
+100

You can specify your own TypeScript folder path in your settings.json where you can specify your own lib.*.d.ts files using the typescript.tsdk option.

{
  "typescript.tsdk": "node_modules/typescript/lib"
}
Sign up to request clarification or add additional context in comments.

9 Comments

While I followed your instructions and put my .d.ts with the other .d.ts files of typescript, VSCode still says the type of defineModule is any. Also, VSCode was trying to get the node_modules/typescript/lib using a path relative to the current workspace instead of the Roaming/npm/node_modules. I have temporarily fixed the problem using an absolute path, though I would greatly appreciate it, if you could also point me in the right direction towards fixing this issue.
Are there any further actions I have to do?
I think if you had typescript installed globally you'd be able to circumvent this by having it point to the globally defined TypeScript installation. Obviously that'd create problems if your projects are pointing to different versions of TypeScript though.
Still a bit unclear, if I should create a new file (do I have to declare it?) under the lib directory or if I should use one of the existing files (which one?). Should I use export or declare? Could you please show an example exporting the defineModule function?
Oh, I figured you already knew how to create your own *.d.ts files and would just add it in there. There are a few examples that show how you can declare and export there elsewhere though stackoverflow.com/questions/21247278/about-d-ts-in-typescript
|
1

Acknowledgment

This answer was mostly inspired by mootrichard's answer, but since it had to be modified, to work with my project, I am also adding this answer.

Solution

If you press F12 on a global JavaScript function (i.e. eval) a typing declarations file will appear (lib.es5.d.ts), containing JavaScript documentation. You can just add any extra namespaces or function to that file. But you need to use declare and not export.

Example:

//... Other Global JavaScript Declarations

// JavaScript Number Interface
interface Number {
  //...
}

// JavaScript Date Interface
interface Date {
  //...
}

declare function ezDefine(moduleName: string, generator: *): void;

2 Comments

Am I the only one feeling uncomfortable editing files which belong to the typescript package that essentially is a vscode dependency?
I found this helpful. I used node_modules/@types/node/globals.d.ts and patch-package @types/node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.