tsconfig.json compiler options - Emit Flashcards
What does declaration
compiler option do?
Generate .d.ts files for every TypeScript or JavaScript file inside your project. These .d.ts files are type definition files which describe the external API of your module.
{ "compilerOptions": { "declaration": "true" } }
Source typescriptlang
What does declarationDir
compiler option do?
Offers a way to configure the root directory for where declaration files are emitted.
{ "compilerOptions": { "declaration": "true", "declarationDir": "./types" } }
Source typescriptlang
What does declarationMap
compiler option do?
Generates a source map for .d.ts files
which map back to the original .ts
source file. This will allow editors such as VS Code to go to the original .ts file when using features like Go to Definition.
You should strongly consider turning this on if you’re using project references.
{ "compilerOptions": { "declarationMap": true } }
Source typescriptlang
What does downlevelIteration
compiler option do?
Downleveling
is TypeScript’s term for transpiling to an older version of JavaScript. This flag is to enable support for a more accurate implementation of how modern JavaScript iterates through new concepts in older JavaScript runtimes.
ECMAScript 6 added several new iteration primitives: the for / of
loop (for (el of arr)
), Array spread ([a, ...b]
), argument spread (fn(...args)
), and Symbol.iterator
. downlevelIteration
allows for these iteration primitives to be used more accurately in ES5 environments if a Symbol.iterator
implementation is present.
{ "compilerOptions": { "downlevelIteration": "true" } }
Source typescriptlang
What does emitBOM
compiler option do?
Controls whether TypeScript will emit a byte order mark (BOM) when writing output files. Some runtime environments require a BOM to correctly interpret a JavaScript files; others require that it is not present.
The default value of false
is generally best unless you have a reason to change it.
{ "compilerOptions": { "emitBOM": "true" } }
Source typescriptlang
What does emitDeclarationOnly
compiler option do?
Only emit .d.ts
files; do not emit .js
files.
This setting is useful in two cases:
- You are using a transpiler other than TypeScript to generate your JavaScript.
- You are using TypeScript to only generate d.ts files for your consumers.
{ "compilerOptions": { "emitDeclarationOnly": true } }
Source typescriptlang
What does importHelpers
compiler option do?
For certain downleveling operations, TypeScript uses some helper code for operations like extending class, spreading arrays or objects, and async operations. By default, these helpers are inserted into files which use them. This can result in code duplication if the same helper is used in many different modules.
If the importHelpers
flag is on, these helper functions are instead imported from the tslib module. You will need to ensure that the tslib
module is able to be imported at runtime. This only affects modules; global script files will not attempt to import modules.
{ "compilerOptions": { "importHelpers": "true" } }
Source typescriptlang
What does importsNotUsedAsValues
compiler option do?
This flag controls how import works, there are 3 different options:
remove
: The default
behavior of dropping import statements which only reference types.
preserve
: Preserves all import statements whose values or types are never used. This can cause imports/side-effects to be preserved.
error
: This preserves all imports (the same as the preserve option), but will error when a value import is only used as a type. This might be useful if you want to ensure no values are being accidentally imported, but still make side-effect imports explicit.
This flag works because you can use import type to explicitly create an import statement which should never be emitted into JavaScript.
Source typescriptlang
What does inlineSourceMap
compiler option do?
When set, instead of writing out a .js.map
file to provide source maps, TypeScript will embed the source map content in the .js files. Although this results in larger JS files, it can be convenient in some scenarios. For example, you might want to debug JS files on a webserver that doesn’t allow .map files to be served.
Mutually exclusive with sourceMap.
Source typescriptlang
What does inlineSources
compiler option do?
When set, TypeScript will include the original content of the .ts file as an embedded string in the source map (using the source map’s sourcesContent
property). This is often useful in the same cases as inlineSourceMap.
Requires either sourceMap or inlineSourceMap to be set.
Source typescriptlang
What does mapRoot
compiler option do?
Specify the location where debugger should locate map files instead of generated locations. This string is treated verbatim inside the source-map, for example:
{ "compilerOptions": { "sourceMap": true, "mapRoot": "https://my-website.com/debug/sourcemaps/" } }
Source typescriptlang
What does noEmit
compiler option do?
Do not emit compiler output files like JavaScript source code, source-maps or declarations.
This makes room for another tool like Babel, or swc to handle converting the TypeScript file to a file which can run inside a JavaScript environment.
You can then use TypeScript as a tool for providing editor integration, and as a source code type-checker.
Source typescriptlang
What does noEmitHelpers
compiler option do?
Instead of importing helpers with importHelpers, you can provide implementations in the global scope for the helpers you use and completely turn off emitting of helper functions.
Source typescriptlang
What does noEmitOnError
compiler option do?
Do not emit compiler output files like JavaScript source code, source-maps or declarations if any errors were reported.
This defaults to false
, making it easier to work with TypeScript in a watch-like environment where you may want to see results of changes to your code in another environment before making sure all errors are resolved.
Source typescriptlang
What does outDir
compiler option do?
If specified, .js
(as well as .d.ts
, .js.map
, etc.) files will be emitted into this directory. The directory structure of the original source files is preserved; see rootDir if the computed root is not what you intended.
If not specified, .js
files will be emitted in the same directory as the .ts
files they were generated from.
{ "compilerOptions": { "outDir": "dist" } }
Source typescriptlang