typescript compile string to js

Solutions on MaxInterview for typescript compile string to js by the best coders in the world

showing results for - "typescript compile string to js"
Dario
12 Oct 2020
1// compile.ts
2
3import * as ts from "typescript";
4
5function tsCompile(source: string, options: ts.TranspileOptions = null): string {
6    // Default options -- you could also perform a merge, or use the project tsconfig.json
7    if (null === options) {
8        options = { compilerOptions: { module: ts.ModuleKind.CommonJS }};
9    }
10    return ts.transpileModule(source, options).outputText;
11}
12
13// Make sure it works
14const source = "let foo: string  = 'bar'";
15
16let result = tsCompile(source);
17
18console.log(result); // var foo = 'bar';
19