Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

TypeScript three slash instructions


May 07, 2021 TypeScript


Table of contents


TypeScript three slash instructions

A three-slash instruction is a single-line comment that contains a single XML label. The contents of the comment are used as compiler instructions.

The three slash instruction can only be placed at the top of the file containing it. O nly one or more lines of comments can appear before a three-slash instruction, which includes other three-slash instructions. If they appear after a statement or declaration, they are treated as normal one-line comments and do not have a special meaning.

/// <reference path="..." />

/// <reference path="..." /> is the most common of the three slash instructions. It is used to declare dependencies between files.

Three slash references tell the compiler to introduce additional files during compilation.

When --out or --outFile it can also be used as a way to adjust the order of output. The position of the file in the output file content is consistent with the preprocessed input order.

Preprocess the input file

The compiler preprocesses the input file to resolve all three slash reference instructions. During this process, additional files are added to the compilation process.

This process starts with some root files; they are either files specified on the command line or files in the "files" tsconfig.json T hese root files are preprocessed in the order specified. B efore a file is added to the list, all three slash references it contains are processed, as well as the targets they contain. Three slash references are resolved in a depth-first manner in the order in which they appear in the file.

A three-slash reference path is relative to the file that contains it, if not the root file.

Error

References to files that do not exist report errors. A file that uses a three-slash instruction to refer to itself will report an error.

Use --noResolve

If --noResolve compilation option is specified, three slash references are ignored;

/// <reference no-default-lib="true"/>

This instruction marks a file as the default library. You'll lib.d.ts file and its different variants.

This instruction tells the compiler not to include this default library (for example, lib.d.ts This is similar to using --noLib line.

Also note that when --skipDefaultLibCheck the compiler only ignores the check for files with /// <reference no-default-lib="true"/>

/// <amd-module />

The AMD modules generated by default are anonymous. However, problems can arise when some tools need to handle the generated module, such r.js

amd-module instruction allows the compiler to pass in an optional module name:

amdModule.ts
///<amd-module name='NamedModule'/>
export class C {
}

This will NamedModule into the AMD define function:

amdModule.js
define("NamedModule", ["require", "exports"], function (require, exports) {
    var C = (function () {
        function C() {
        }
        return C;
    })();
    exports.C = C;
});

/// <amd-dependency />

Note: This instruction has been discarded. U se import "moduleName"; Statement instead.

/// <amd-dependency path="x" /> the compiler that there is a non-TypeScript module dependency that needs to be injected as part of the target module require call.

amd-dependency instruction can also be brought with an optional name property;

/// <amd-dependency path="legacy/moduleA" name="moduleA"/>
declare var moduleA:MyType
moduleA.callStuff()

Generated JavaScript code:

define(["require", "exports", "legacy/moduleA"], function (require, exports, moduleA) {
    moduleA.callStuff()
});