📜  typescript 将字符串编译为 js - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:08.985000             🧑  作者: Mango

代码示例1
// compile.ts

import * as ts from "typescript";

function tsCompile(source: string, options: ts.TranspileOptions = null): string {
    // Default options -- you could also perform a merge, or use the project tsconfig.json
    if (null === options) {
        options = { compilerOptions: { module: ts.ModuleKind.CommonJS }};
    }
    return ts.transpileModule(source, options).outputText;
}

// Make sure it works
const source = "let foo: string  = 'bar'";

let result = tsCompile(source);

console.log(result); // var foo = 'bar';