1

I have some logic saved in a string and I need to convert it to a runable function. I have Interface already setup to make it strongly typed in Typescript, but it seem to not work.

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);
});

Should write: Test SUCCESS

2
  • Is that the actual text that you need to work with or you can change the syntax so that it will be real javascript? Commented Jul 12, 2017 at 19:30
  • Syntax can be changed, I just wrote some test code. Commented Jul 12, 2017 at 19:32

1 Answer 1

1

you evaluate javascript stored in strings via eval.

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));

should print as you expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.