我将一些逻辑保存在字符串中,需要将其转换为可运行的函数。我已经将界面设置为在Typescript中进行强类型输入,但它似乎不起作用。
interface IInterface {
Run(data: string): Promise<string>;
}
let code: string = `
return new class{
let Run = function(data) {
console.log(data);
return "SUCCESS";
};
}
`;
let obj: IInterface = Function(code) as IInterface;
obj.Run("Test ").then((result: string ) => {
console.log(result);
});
应该这样写:测试成功
发布于 2017-07-12 19:48:34
您可以通过eval
评估存储在字符串中的javascript。
interface IInterface {
run(data: string): Promise<string>;
}
var f: IInterface = eval(`({run: function(data) { console.log(data); return Promise.resolve("SUCCESS"); } })`);
f.run("hello world").then(x=>console.log(x));
应该像您期望的那样打印。
https://stackoverflow.com/questions/45066023
复制相似问题