liveBook · Manning

Target property

As explained in Essential TypeScript 5, Third Edition

The target property is a TypeScript compiler configuration setting that selects the JavaScript language version for generated code and default library typings.

Set compilerOptions.target in tsconfig.json to choose the JavaScript version the TypeScript compiler targets. This lets you write newer syntax while generating code for an older runtime. For example, with "target": "ES5", a let declaration becomes var and an arrow function becomes a regular function in dist/index.js. section 5.6

Target ES5

In tools/tsconfig.json, set "compilerOptions": { "target": "ES5", "outDir": "./dist", "rootDir": "./src" }. A src/index.ts function written as let printMessage = (msg: string): void => console.log(`Message: ${msg}`) is emitted using var and function syntax. The compiler also removes the type annotation. section 5.6

Target also determines which JavaScript library declarations the compiler includes by default. With ES5 selected, new Map() in src/index.ts produces TS2583 because Map belongs to ES2015. You can choose a later target or explicitly set lib to include the declarations you need. section 5.7

Listing 5.22 Changing the configuration in the tsconfig.json file in the tools folder
{
    "compilerOptions": {
        "target": "ES5",
        "outDir": "./dist",
        "rootDir": "./src",
        "noEmitOnError": true,
        "lib": ["es5", "dom", "es2015.collection"]
    }
}

This configuration keeps ES5 output while adding ES2015 collection typings alongside ES5 and DOM typings. section 5.7

Adding a library declaration does not add that feature to the runtime: an ES5 build using Map still needs a runtime that provides Map or a polyfill. In the demonstrated module setup, ES5 output uses CommonJS, while switching the target to ES2022 leaves import and export in ECMAScript module form. Running that output in Node.js also requires compatible package configuration and an explicit .js extension in the import path. section 5.7 section 5.8

A React build can separate type checking from JavaScript transformation. With noEmit enabled and Babel transforming the code, changing target to ES2022 affects which language features TypeScript accepts; Babel handles the transformation. section 20.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through configuration, transformed syntax, a missing-library error, and module output; distinguishes declarations from runtime support.
Examples
Uses ES5 arrow-function output, a Map error, an ES2022 module change, and a React configuration.
What it assumes
Assumes familiarity with JavaScript syntax and introduces target and lib through concrete TypeScript configuration and compiler output.
What you can do after
Operate/configure it to choose an output version, adjust typings deliberately, and check that the runtime supports the resulting code.
How it connects
Connects target to tsconfig.json, ECMAScript versions, lib declarations, polyfills, CommonJS, ECMAScript modules, and Babel.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.