Issue
Is it possible to save my Typescript code in a string and evaulate it during run time? For simple example:
let code: string = `({
Run: (data: string): string => {
console.log(data); return Promise.resolve("SUCCESS"); }
})`;
Then run it like this:
let runnalbe = eval(code);
runnable.Run("RUN!").then((result:string)=>{console.log(result);});
Should print:
RUN!SUCCESS
Solution
The official TypeScript compiler API provides a way to transpile source strings:
import * as ts from "typescript";
let code: string = `({
Run: (data: string): string => {
console.log(data); return Promise.resolve("SUCCESS"); }
})`;
let result = ts.transpile(code);
let runnalbe :any = eval(result);
runnalbe.Run("RUN!").then((result:string)=>{console.log(result);});
Answered By - Saravana
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.