Skip to main content
The @secure-exec/typescript companion package runs the TypeScript compiler inside a sandbox for safe type checking and compilation of untrusted code.

Install

pnpm add @secure-exec/typescript

Setup

import { createNodeDriver, createNodeRuntimeDriverFactory } from "secure-exec";
import { createTypeScriptTools } from "@secure-exec/typescript";

const ts = createTypeScriptTools({
  systemDriver: createNodeDriver(),
  runtimeDriverFactory: createNodeRuntimeDriverFactory(),
});
Options:
OptionTypeDefaultDescription
systemDriverSystemDriverrequiredCompiler runtime capabilities and filesystem view
runtimeDriverFactoryNodeRuntimeDriverFactoryrequiredCreates the compiler sandbox
memoryLimitnumber512Compiler isolate memory cap in MB
cpuTimeLimitMsnumberCompiler CPU time budget in ms
compilerSpecifierstring"typescript"Module specifier for the TypeScript compiler

Type-check a source string

const result = await ts.typecheckSource({
  sourceText: "const x: number = 'hello';",
});

console.log(result.success); // false
console.log(result.diagnostics[0].message);
// "Type 'string' is not assignable to type 'number'."

Type-check a project

const result = await ts.typecheckProject({
  cwd: "/app",
  configFilePath: "/app/tsconfig.json",
});

for (const d of result.diagnostics) {
  console.log(`${d.filePath}:${d.line} ${d.message}`);
}

Compile a source string

const result = await ts.compileSource({
  sourceText: "const x: number = 42; export default x;",
});

console.log(result.outputText);
// "const x = 42; export default x;"

Compile a project

const result = await ts.compileProject({
  cwd: "/app",
});

console.log(result.emittedFiles); // ["/app/dist/index.js", ...]
console.log(result.success);     // true

Diagnostic shape

type TypeScriptDiagnostic = {
  code: number;
  category: "error" | "warning" | "suggestion" | "message";
  message: string;
  filePath?: string;
  line?: number;
  column?: number;
};