Skip to content

Commit cf75419

Browse files
authored
Support loading additional TS lib files (denoland/deno#3863)
Fixes denoland/deno#3726 This PR provides support for referencing other lib files (like lib.dom.d.ts that are not used by default in Deno.
1 parent ce97f19 commit cf75419

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

manual.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ command line:
174174
$ deno types
175175
```
176176

177+
The output is the concatenation of three library files that are built into Deno:
178+
179+
- [lib.deno.ns.d.ts](https://github.com/denoland/deno/blob/master/cli/js/lib.deno.ns.d.ts)
180+
- [lib.deno.shared_globals.d.ts](https://github.com/denoland/deno/blob/master/cli/js/lib.deno.shared_globals.d.ts)
181+
- [lib.deno.window.d.ts](https://github.com/denoland/deno/blob/master/cli/js/lib.deno.window.d.ts)
182+
177183
[This is what the output looks like.](https://github.com/denoland/deno/blob/master/cli/js/lib.deno_runtime.d.ts)
178184

179185
### Reference websites
@@ -640,6 +646,100 @@ reference directive. If Deno used this, it would interfere with the behavior of
640646
the TypeScript compiler. Deno only looks for the directive in JavaScript (and
641647
JSX) files.
642648

649+
### Referencing TypeScript library files
650+
651+
When you use `deno run`, or other Deno commands which type check TypeScript,
652+
that code is evaluated against custom libraries which describe the environment
653+
that Deno supports. By default, the compiler runtime APIs which type check
654+
TypeScript also use these libraries (`Deno.compile()` and `Deno.bundle()`).
655+
656+
But if you want to compile or bundle TypeScript for some other runtime, you may
657+
want to override the default libraries. In order to do this, the runtime APIs
658+
support the `lib` property in the compiler options. For example, if you had
659+
TypeScript code that is destined for the browser, you would want to use the
660+
TypeScript `"dom"` library:
661+
662+
```ts
663+
const [errors, emitted] = Deno.compile(
664+
"main.ts",
665+
{
666+
"main.ts": `document.getElementById("foo");\n`
667+
},
668+
{
669+
lib: ["dom", "esnext"]
670+
}
671+
);
672+
```
673+
674+
For a list of all the libraries that TypeScript supports, see the
675+
[`lib` compiler option](https://www.typescriptlang.org/docs/handbook/compiler-options.html)
676+
documentation.
677+
678+
**Don't forget to include the JavaScript library**
679+
680+
Just like `tsc`, when you supply a `lib` compiler option, it overrides the
681+
default ones, which means that the basic JavaScript library won't be included
682+
and you should include the one that best represents your target runtime (e.g.
683+
`es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020` or `esnext`).
684+
685+
#### Including the `Deno` namespace
686+
687+
In addition to the libraries that are provided by TypeScript, there are four
688+
libraries that are built into Deno that can be referenced:
689+
690+
- `deno.ns` - Provides the `Deno` namespace.
691+
- `deno.shared_globals` - Provides global interfaces and variables which Deno
692+
supports at runtime that are then exposed by the final runtime library.
693+
- `deno.window` - Exposes the global variables plus the Deno namespace that are
694+
available in the Deno main worker and is the default for the runtime compiler
695+
APIs.
696+
- `deno.worker` - Exposes the global variables that are available in workers
697+
under Deno.
698+
699+
So to add the Deno namespace to a compilation, you would include the `deno.ns`
700+
lib in the array. For example:
701+
702+
```ts
703+
const [errors, emitted] = Deno.compile(
704+
"main.ts",
705+
{
706+
"main.ts": `document.getElementById("foo");\n`
707+
},
708+
{
709+
lib: ["dom", "esnext", "deno.ns"]
710+
}
711+
);
712+
```
713+
714+
**Note** that the Deno namespace expects a runtime environment that is at least
715+
ES2018 or later. This means if you use a lib "lower" than ES2018 you will get
716+
errors logged as part of the compilation.
717+
718+
#### Using the triple slash reference
719+
720+
You do not have to specify the `lib` in just the compiler options. Deno supports
721+
[the triple-slash reference to a lib](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-lib-).
722+
and could be embedded in the contents of the file. For example of you have a
723+
`main.ts` like:
724+
725+
```ts
726+
/// <reference lib="dom" />
727+
728+
document.getElementById("foo");
729+
```
730+
731+
It would compiler without errors like this:
732+
733+
```ts
734+
const [errors, emitted] = Deno.compile("./main.ts", undefined, {
735+
lib: ["esnext"]
736+
});
737+
```
738+
739+
**Note** that the `dom` library conflicts with some of the default globals that
740+
are defined in the default type library for Deno. To avoid this, you need to
741+
specify a `lib` option in the compiler options to the runtime compiler APIs.
742+
643743
### Testing if current file is the main program
644744

645745
To test if the current script has been executed as the main input to the program

0 commit comments

Comments
 (0)