TypeScript Compiler API

ts.CompilerOptions

The typed option object accepted by compiler API functions. API enum values such as ts.ScriptTarget.ES2022 differ from string values in tsconfig.json. Convert JSON compiler options with convertCompilerOptionsFromJson and inspect its errors; parsing an entire tsconfig requires a separate configuration parser that handles files, extends and includes. Baseline: the Node.js API of TypeScript 5.8.3. The examples are CommonJS tooling scripts, not browser or CodeRunner snippets. TypeScript must be installed locally.

Formal Syntax & Structure

typescript syntaxtypescript
interface ts.CompilerOptions; ts.convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName?)

Working Example

Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const raw = { target: "ES2022", module: "CommonJS", strict: true };const converted = ts.convertCompilerOptionsFromJson(raw, process.cwd());assert.equal(converted.errors.length, 0);assert.equal(converted.options.target, ts.ScriptTarget.ES2022);assert.equal(converted.options.module, ts.ModuleKind.CommonJS);console.log(ts.ScriptTarget[converted.options.target], converted.options.strict);
// Run: node example.cjs// Expected stdout: ES2022 true
// Separate variation in a fresh directory.// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const result = ts.convertCompilerOptionsFromJson({ target: "invalid" }, process.cwd());assert.deepEqual(result.errors.map(d => d.code), [6046]);console.log(result.errors[0].code);
// Run: node example.cjs// Expected stdout: 6046
TypeScript Reference