Search code examples
typescriptdeno

Is there a specific way to use the import map for type information?


I am new to Deno and I am figuring out the import map feature. Currently, my import map for express looks like this:

"imports": {
    "express": "npm:express@4.18.2"
},

And I am importing express into my project like this:

// @deno-types="npm:@types/express@4.17.15"
import express from "express";

The code runs just fine but I find it a bit annoying to have to import the typing in every file I want to use the express module in. Is there a standard way in Deno to import the typings in one place and not have to reference them directly in individual files?


Solution

  • That's considered an antipattern especially in Deno, and I'm not sure if that's even possible to achieve. Deno's philosophy is to make everything explicit and easy to analyze, debug and backtrack.

    By omitting the import, you would:

    • Obfuscate where the express identifier comes from
    • Make it harder to tell which dependencies are used in your file as you would need to scan through the entire file
    • Make it harder for linting tools to work properly
    • Limit namespace collision handling, as you wouldn't be able to rename the express import anymore. If you have another dependency that also exports a member called express, things could start to become confusing

    Also, as of September 2023, Deno is able to autoimport dependencies. For example, in VSCode, if you reference something that is not imported in your file, Deno is able to detect it and add an import statement automatically for you.