@@ -580,6 +580,48 @@ import { test, assertEquals } from "./deps.ts";
580
580
This design circumvents a plethora of complexity spawned by package management
581
581
software, centralized code repositories, and superfluous file formats.
582
582
583
+ ### Using external type definitions
584
+
585
+ Deno supports both JavaScript and TypeScript as first class languages at
586
+ runtime. This means it requires fully qualified module names, including the
587
+ extension (or a server providing the correct media type). In addition, Deno has
588
+ no "magical" module resolution.
589
+
590
+ The out of the box TypeScript compiler though relies on both extension-less
591
+ modules and the Node.js module resolution logic to apply types to JavaScript
592
+ modules.
593
+
594
+ In order to bridge this gap, Deno supports compiler hints that inform Deno the
595
+ location of ` .d.ts ` files and the JavaScript code they relate to. A compiler
596
+ hint looks like this:
597
+
598
+ ``` ts
599
+ // @deno-types="./foo.d.ts"
600
+ import * as foo from " ./foo.js"
601
+ ```
602
+
603
+ Where the hint effects the next ` import ` statement (or ` export ... from `
604
+ statement) where the value of the ` @deno-types ` will be substituted at compile
605
+ time instead of the specified module. Like in the above example, the Deno
606
+ compiler will load ` ./foo.d.ts ` instead of ` ./foo.js ` . Deno will still load
607
+ ` ./foo.js ` when it runs the program.
608
+
609
+ ** Not all type definitions are supported.**
610
+
611
+ Deno will use the compiler hint to load the indicated ` .d.ts ` files, but some
612
+ ` .d.ts ` files contain unsupported features. Specifically, some ` .d.ts ` files
613
+ expect to be able to load or reference type definitions from other packages
614
+ using the module resolution logic. For example a type reference directive to
615
+ include ` node ` , expecting to resolve to some path like
616
+ ` ./node_modules/@types/node/index.d.ts ` . Since this depends on non-relative
617
+ "magical" resolution, Deno cannot resolve this.
618
+
619
+ ** Why not use the triple-slash type reference?**
620
+
621
+ The TypeScript compiler supports triple-slash directives, including a type
622
+ reference directive. If Deno used this, it would interfere with the behavior
623
+ of the TypeScript compiler.
624
+
583
625
### Testing if current file is the main program
584
626
585
627
To test if the current script has been executed as the main input to the program
0 commit comments