TypeScript transpileModule
Concept and boundary
transpileModule processes an isolated source string and can emit JavaScript even when a whole-program checker would reject it. reportDiagnostics can report some syntax and option issues but does not enable full semantic analysis. Use createProgram for cross-file contracts.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: transpile-module
Revisit reference fixture ts-api-transpile-module (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const source = 'const n: number = "bad"; console.log(typeof n);';const result = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS }, fileName: "input.ts", reportDiagnostics: true });assert.equal(result.diagnostics.length, 0);assert(!result.outputText.includes(": number"));const fs = require("node:fs");fs.writeFileSync("generated.cjs", result.outputText);const output = require("node:child_process").execFileSync(process.execPath, ["generated.cjs"], { encoding: "utf8" }).trim();assert.equal(output, "string");console.log(result.diagnostics.length, output);Expected stdout (exit 0):
0 stringWorkshop 2: get-pre-emit-diagnostics
Revisit reference fixture ts-api-get-pre-emit-diagnostics (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const codes = ts.getPreEmitDiagnostics(program).map(d => d.code);assert.deepEqual(codes, [2322]);console.log(codes.join(","));Expected stdout (exit 0):
2322Practice and recap
Compare the same incompatible assignment under transpilation and whole-program checking.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TranspileModule limitations — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes TranspileModule limitations?
What does workshop 1 (transpile-module) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const source = 'const n: number = "bad"; console.log(typeof n);';const result = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS }, fileName: "input.ts", reportDiagnostics: true });assert.equal(result.diagnostics.length, 0);assert(!result.outputText.includes(": number"));const fs = require("node:fs");fs.writeFileSync("generated.cjs", result.outputText);const output = require("node:child_process").execFileSync(process.execPath, ["generated.cjs"], { encoding: "utf8" }).trim();assert.equal(output, "string");console.log(result.diagnostics.length, output);What does workshop 2 (get-pre-emit-diagnostics) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const codes = ts.getPreEmitDiagnostics(program).map(d => d.code);assert.deepEqual(codes, [2322]);console.log(codes.join(","));