Answers for "javascript compile typescript"

0

typescript compile string to js

// 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';
Posted by: Guest on May-03-2021
0

how to compile typescript to javascript es6

Run $tsc —init (in the folder containing the typescript file to compile)
(this creates a tsconfig.json file which has a bunch of json settings)
Navigate to tsconfig.json
 and 
Change from “target”: “es5”  to “target”: “es6”
Recompile the typescript file by tsc index and the index.js file will have es
Posted by: Guest on November-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language