TypeScript Compiler API

ts.preProcessFile()

Performs lightweight source preprocessing to discover reference directives and candidate module imports without building a semantic program. It does not resolve files, prove runtime dependencies or replace parsing. Optional flags control import scanning and JavaScript-style import detection. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.

Formal Syntax & Structure

typescript syntaxtypescript
ts.preProcessFile(sourceText: string, readImportFiles?: boolean, detectJavaScriptImports?: boolean): ts.PreProcessedFileInfo

Parameters & Attributes

NameTypeDescription
sourceTextstringOriginal source text.
readImportFilesbooleanScan module imports (defaults to true).
detectJavaScriptImportsbooleanInclude supported JavaScript import patterns.

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 text='/// <reference path="./types.d.ts" />\nimport { x } from "./dep";\nexport { x };';const info=ts.preProcessFile(text); assert.deepEqual(info.referencedFiles.map(x=>x.fileName),["./types.d.ts"]); assert.deepEqual(info.importedFiles.map(x=>x.fileName),["./dep"]);assert.deepEqual(ts.preProcessFile(text,false).importedFiles,[]);const js=ts.preProcessFile('const x = require("pkg");',true,true); assert.deepEqual(js.importedFiles.map(x=>x.fileName),["pkg"]);console.log("path reference; import; optional require detection");
// Run: node example.cjs// Expected stdout: path reference; import; optional require detection
TypeScript Reference