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
