TypeScripttsconfig options
"target"
The JavaScript version the compiler turns your code into.
Example
#JSON
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"]
}
}Definition and usage
#The target option specifies which language version the compiler should generate, for example ES2017, ES2022, or ESNext. Newer syntax that isn’t available in the chosen version is rewritten into an older form, where possible.
The value also affects the default set of standard library types (lib). With ES2022, for example, the types for Array.prototype.at() and Object.hasOwn() are available.
In projects with a bundler (Vite, Next.js, webpack), the build tool usually does the transpilation, and target in tsconfig is mainly used for type checking. Set it to a version supported by the browsers or the Node.js environment you’re targeting.
Syntax
#Syntax
"target": "ES2022"Good practices
#- For modern browsers and Node.js 20+,
ES2022is a safe choice. - The
liboption lets you choose exactly which types are available, e.g. addDOMin a browser project.
Related entries
#- "module"The module format of the generated code and how imports are understood.
- "strict"Turns on a set of the most important, rigorous compiler checks.
- "outDir" and "rootDir"The folder where compiled code goes, and the folder of the sources.
- "lib"Selects the built-in environment types: the JavaScript version, browser APIs, and individual new language features.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.