How to use the createWatchProgram function from typescript

Find comprehensive JavaScript typescript.createWatchProgram code examples handpicked from public code repositorys.

typescript.createWatchProgram is a method to create a TypeScript program that can watch for file changes and recompile the program as needed.

610
611
612
613
614
615
616
617
618

enableStatisticsAndTracing();
updateOutputs();
applySyntheticOutPaths();

const program = ts.createWatchProgram(host);

// early return to prevent from declaring more variables accidentially. 
return { program, applyArgs, setOutput, formatDiagnostics, flushWatchEvents, invalidate, postRun, printFSTree: filesystem.printTree };
fork icon26
star icon48
watch icon4

+ 711 other calls in file

How does typescript.createWatchProgram work?

typescript.createWatchProgram is a method provided by TypeScript which creates a compiler host to compile TypeScript files in watch mode, meaning it will continuously monitor the files for changes and re-compile them when necessary. It returns a WatchOfConfigFile object that can be used to interact with the watch program, such as to get diagnostics and emit compiled files.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const ts = require("typescript");

// Define the options for the TypeScript compiler
const compilerOptions = {
  target: ts.ScriptTarget.ES2015,
  module: ts.ModuleKind.CommonJS,
};

// Create the file names array containing the TypeScript files to watch
const fileNames = ["app.ts"];

// Create the host object for the compiler
const host = ts.createWatchCompilerHost(
  fileNames,
  compilerOptions,
  ts.sys,
  ts.createSemanticDiagnosticsBuilderProgram,
  undefined,
  undefined
);

// Create the program and start watching for changes
ts.createWatchProgram(host);

This code sets up the TypeScript compiler with the desired options and an array of files to watch for changes. It then creates a watchCompilerHost object, which is used to create a watchProgram that will watch for changes to the specified files and recompile the project when necessary.

Other functions in typescript

Sorted by popularity

function icon

typescript.SyntaxKind is the most popular function in typescript (82777 examples)