How To Build Typescript Code In Js Using A Function?
Trying to build typescript code in JS so i can display on UI for user to play with the code , is there a way to create this code as a typescript instead of text so it compile as we
Solution 1:
You can create the Typescript as executable by doing something like -
const executableTypescript = newFunction(typescriptDataAsText);
The typescript code in the string will already be compiled at this point.
To execute it, you just call the newly created function like -
executableTypescript();
Check by running the below snippet that the Typescript code of logging the message in the variable is executed.
Note: For your case, if the modelData
and more such Typescript data is included, it should be a part of a new module since it has import statements and they are required to be on top of a module.
var path = "/filepath"var data = [
{
name: "IParam"
},
{
name: "IError"
}
]
functioncreateInterfaces(path, data){
const imports = data.map(d => d.name).join(', ');
return`import { ${imports} } from '${path}';\n\n`;
}
functionbuildTypescript(data) {
// To include this data as part of your text, make sure that it's a part of a new moduleconst modelData = createInterfaces(path,data);
const text = `
let message = "Typecript executed";\n\n
console.log(message)`;
return text;
}
const typescriptDataAsText = buildTypescript(data);
const executableTypescript = newFunction(typescriptDataAsText);
executableTypescript()
Post a Comment for "How To Build Typescript Code In Js Using A Function?"