Reputation: 1415
Somewhat inspired by Jest and some other tools, I'm working on a program which loads small source files, written in TypeScript, and compiles and runs them in a separate VM context within Node.js. (No, it's not a competitor to Jest).
Similar to what Jest does with describe
and test
, this program "injects" certain predefined globals into the script before running it, so that the script doesn't need to explicitly import those symbols. This works surprisingly well, except for the dreaded "red squiggles" in VSCode.
The problem is that VSCode doesn't know about the injected symbols. Now, with Jest the solution is to install @types/jest
into your projects, and then add a "types": ["jest"]
property to your project's tsconfig.json
. However, I really don't want to have to maintain a separate package just for the ambient types, and I don't want to have to bother the DefinitelyTyped people to add support for my little project.
Is there a way I can configure my project so that VSCode (or rather the TypeScript language service) knows that certain types are always present? I've tried mucking around with tsconfig.json
, /// reference
, "types" in package.json, and so on, but nothing I have tried seems to work.
To be clear, what I am talking about is a relationship between two separate npm packages - a "providing" package that provides the type definitions, and a "consuming" package that uses them. So just as in Jest, a package which depends on Jest can have a test file that uses Jest symbols without needing to explicitly import them.
GitHub repo is here if anyone wants to take a look: https://github.com/viridia/overrun
Upvotes: 3
Views: 223
Reputation: 2955
I think in this case you'd have to create a global.d.ts
file that types out the your globals.
See the TS docs on globals. It doesn't just give you types out of the box by installing. You can however tell people that if they want TS support to copy-paste a global.d.ts
file into their project and that'll work.
Upvotes: 1
Reputation: 697
EDIT: You could always have all your types in a .ts or .js file and import it into your main file. Not so suggested route is that you could declare your types as globals in the imported or main files.
ORIGINAL: If you do not want an explicit type doc have you checked out JSDoc? I dont know of how much help it could be but I would suggest atleat having a look if you haven't, a simple example looks as follows:
/**
* @param {string} somebody - Somebody's name.
*/
function sayHello(somebody) {
alert('Hello ' + somebody);
}
When calling the function it will recommend type and variables with the description you choose. It won't cast errors like typescript but helps people figure out how to use the function.
Read more here
Upvotes: 0