How to pull typings in Deno

tldr

Install denoland.vscode-deno extension 1. Then enable Deno in a particular project, like

<proj-root>/.vscode/settings.json:
{
  "deno.enable": true, // enables extension
  "deno.lint": true, // inline deno lint diagnostics, requires `deno.unstable`
  "deno.unstable": true // also provides unstable type declarations in VS Code
}

Starting with v2.3.0, you can also use the deno: Init wizard 2: enter image description here

To integrate a TS workspace version instead of VS Code built-in one, take a look at linked docs.

1This one is deprecated.
2Note: In a new empty project, at least one source file needs to exist before the wizard works properly.


More details

What does the extension do?

  • permits explicit .ts extensions in VS Code
  • resolves URL import specifiers like "https://deno.land/[email protected]/http/server.ts"
  • integrates Deno runtime types (e.g. Deno.writeFile)

How to resolve types for .ts modules

With above extension, VS Code permits .ts file extensions for imports and resolves URL imports to a local disk cache. The compiler can use these cached type definitions to do checks. Last, fetch all sources and restart TS server / reload VS Code:

deno cache https://deno.land/std/http/server.ts # fetch and compile from URL 
# or main project file
deno cache <your main file>.ts # fetches all its dependencies

server.ts is part of the Standard Library, which is just a more strictly maintained collection of remote .ts modules, so it also will be typed correctly.

How to resolve types for .js modules

Deno provides additional ways to reference .d.ts files for .js files.

Specify type definition at import code location:
// @deno-types="./foo.d.ts"
import * as foo from "./foo.js";
Specify type definition at host code location:
/// <reference types="./foo.d.ts" />
export const foo = "foo";

Alternative: Deno can read a custom X-TypeScript-Types HTTP header from remote imports.

How to use a custom tsconfig.json

A given tsconfig.json file in a project is merged with the Deno default config, e.g.:
{
  "compilerOptions": {
    // set a custom, deviant value
    "noImplicitAny": false // deno default is true
    // (this is just an example - use strong types, whenever possible)
  }
}
Include -c option, so VS Code and Deno CLI have the same compiler settings:
deno run -c ./tsconfig.json main.ts 

How to use --unstable types

The easiest option is to set "deno.unstable": true (PR) in settings.json and restart VS Code, see the tldr section.

Manual alternative

cd <your-project>
deno types --unstable > deno.runtime.d.ts
touch tsconfig.json # (1); jsconfig.json for JS projects also possible

VS Code automatically includes deno.runtime.d.ts with an existent tsconfig.json (1).


Breaking Changes

vscode-deno v1.26.0

The extension needs to be explicitly enabled by "deno.enable": true in .vscode/settings.json of the project (default is false). Before, the default had been true.

vscode-deno < 1.25.0

Install Deno types in every case (stable and unstable) - see "How to use --unstable types".


Update: vscode-deno works fairly well.

Below is the current solution:

  1. Add https://github.com/kitsonk/deno_ls_plugin to your workspace. Edit your tsconfig.json and replace paths to http and https imports with the local cache location of the remote dependencies (usually under $HOME/.deno/deps/http and $HOME/.deno/deps/https)
  2. Create a typings/ folder. Run deno --types > typings/deno.d.ts. Deno would output a type definition file for the core API.
  3. Now the problem becomes that VS Code has no idea about fetching remote dependencies when you import. To address this, once you added a new remote import, run deno --prefetch your_file.ts to pull down all the dependencies. To make things simpler, I would suggest keep all your remote dependencies in a single file deps.ts (and run --prefetch on this file) and re-export stuff so that other files in your project could make use of the deps.