node.jstypescriptdeno

How to include Definitely Typed library to my deno project?


How do I use the deno's node compatibility mode with definitely typed modules? I tried this but that didn't seem to work as intended.

/// <reference types='npm:@types/three' />

  1. I simply want the types available globally within my project
  2. I do not want three.js library included in my code, only types

Solution

  • As of Deno v1.26.1, TypeScript is not supported in Node compatibility mode. From the Node Compatibility Mode page in the manual:

    TypeScript support

    Currently, the compatibility mode does not support TypeScript.

    In the upcoming releases we plan to add support for a types field in package.json, to automatically lookup types and use them during type checking.

    In the long term, we'd like to provide ability to consume TypeScript code authored for the Node runtime.


    If you just want the types from @types/three in a normal Deno module, then simply import them like you normally would from a source which utilizes Deno's literal import specifier syntax (such as esm.sh):

    import type { Vector3Tuple } from "https://esm.sh/@types/three/index.d.ts?pin=v96";
    
    const triplet: Vector3Tuple = [2, 2, 2]; // ok
    

    Or all of the types onto a namespace:

    import type * as Three from "https://esm.sh/@types/three/index.d.ts?pin=v96";
    
    const triplet: Three.Vector3Tuple = [2, 2, 2]; // ok