Triple-Slash Directives Flashcards
What are triple-slash directives in TypeScript?
Triple-slash directives in TypeScript are single-line comments containing a single XML tag. The contents of the comment are used as compiler directives. They are only valid at the top of their containing file and can only be preceded by single or multi-line comments, including other triple-slash directives.
Example:
/// <reference path="..." />
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
What is the syntax of a triple-slash directive in TypeScript?
The syntax of a triple-slash directive is as follows:
/// <directive-name path="..." />
For instance, the most common triple-slash directive is:
/// <reference path="..." />
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
What is the purpose of the triple-slash reference
directive in TypeScript?
The /// <reference path="..." />
directive is a declaration of dependency between files. It instructs the compiler to include additional files in the compilation process and serves as a method to order the output when using out or outFile.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
Where can triple-slash directives be placed in a TypeScript file?
Triple-slash directives can only be placed at the top of a TypeScript file. They can only be preceded by single or multi-line comments, including other triple-slash directives. If they are encountered following a statement or a declaration, they are treated as regular single-line comments and hold no special meaning.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
How does the TypeScript compiler process triple-slash directives?
The TypeScript compiler performs a preprocessing pass on input files to resolve all triple-slash reference directives. It starts with a set of root files (specified on the command-line or in the tsconfig.json
file) and preprocesses them in the order they are specified. Before a file is added to the list, all its triple-slash references are processed and their targets included. These references are resolved in a depth-first manner, in the order they appear in the file.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
How is a triple-slash reference path resolved in TypeScript?
A triple-slash reference path in TypeScript is resolved relative to the containing file if a relative path is used. This resolution happens during the preprocessing pass performed by the TypeScript compiler.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
How does /// <reference path="..." />
directive work in TypeScript?
The /// <reference path="..." />
directive in TypeScript serves as a declaration of dependency between files. It instructs the compiler to include additional files in the compilation process and also serves as a method to order the output when using out or outFile.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
What does the --noResolve
compiler flag do in TypeScript?
If the --noResolve
compiler flag is specified in TypeScript, triple-slash references are ignored. They neither result in adding new files, nor change the order of the files provided.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
What is the purpose of /// <reference types="..." />
directive in TypeScript?
Similar to a /// <reference path="..." />
directive, a /// <reference types="..." />
directive in TypeScript declares a dependency on a package. These are typically used when authoring a d.ts
file by hand.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
How does /// <reference lib="..." />
directive work in TypeScript?
This directive allows a file in TypeScript to explicitly include an existing built-in lib file. This is useful for declaration file authors who rely on built-in types, e.g., DOM APIs or built-in JS run-time constructors like Symbol or Iterable.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.
What does /// <reference no-default-lib="true"/>
directive do in TypeScript?
This directive in TypeScript marks a file as a default library and instructs the compiler to not include the default library (i.e., lib.d.ts
) in the compilation. The impact here is similar to passing noLib
on the command line.
“Documentation - Triple-Slash Directives” (typescriptlang.org). Retrieved July 17, 2023.