3

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

3
  • 1
    I thought of a simpler way of asking this question: "Many recent TypeScript packages on npm self-host their own type definitions instead of having a separate @types package on DefinitelyTyped. Is it possible to self-host ambient types? That is, can a tsconfig "types" property reference type definitions that are bundled with the installed package, such that those types are implicitly imported, rather than having to be explicitly imported? Commented Aug 28, 2021 at 5:31
  • 1
    You might be able to get this to work by specifying an alternative typeRoots (e. g. typeRoots: ["@types", "@your-namespace", "./node_modules/or-even-your-package-maybe"]). I'm not sure this will work, hence the comment instead of the answer. Commented Sep 1, 2021 at 19:31
  • 1
    I think that is the approach closest to what I am looking for, but as you say, I'm not sure it will work. It seems like there is a lack of documentation / clarity on exactly how typeroots work - I also asked this same question in the TypeScript discord channels. Commented Sep 2, 2021 at 5:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.