jzbg
jzbg•9mo ago

What is the correct syntax to set the `compilerOptions.types` to use npm @types library

For example: As of now if I want my .ts files to have access to the npm:express packages @types/express I need to always reference in each file // @deno-types="npm:@types/express" I want to update the deno.json compilerOptions.types array to include for example npm:@types/express ... However, when I attempt to include globally, it does not get picked up in the .ts files. In summary, how can I avoid needing to reference the // @deno-types=... in each separate file. I just want to set it once in the deno config. Thanks
1 Reply
Deno AI Helper
Deno AI Helper•9mo ago
I'm sorry, but according to the documentation, there's no explicit way to globally set @deno-types using the compilerOptions.types in the deno.json configuration file. The compilerOptions.types array in the configuration file is used to include type definitions, but it doesn't support the @deno-types directive for npm packages. The @deno-types directive is used to instruct Deno to use a specific type definition file when type checking a JavaScript or TypeScript module. This directive needs to be placed directly above the import statement in the file where the module is imported. If you're using npm packages in Deno, you can use the npm: specifier to import the package and the @deno-types directive to specify the corresponding @types package, if one exists. However, this needs to be done for each file where the npm package is imported. Here's an example:
// @deno-types="npm:@types/express@^4.17"
import express from "npm:express@^4.17";
// @deno-types="npm:@types/express@^4.17"
import express from "npm:express@^4.17";
In this case, the @deno-types directive tells Deno to use the types from the @types/express package when type checking the express module. I understand that this might not be the answer you were hoping for, but currently, this is the way to use npm packages with type definitions in Deno according to the documentation. 🦕