22

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
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • No. Because TypeScript is not JavaScript, and using `eval` will most likely give you a syntax error. – d4nyll Jul 17 '17 at 21:22
  • 2
    Typescript was invented to make your code safer :) Putting code in a string makes it incredibly unsafe :) – Kokodoko Jul 17 '17 at 22:21

3 Answers3

36

The official TypeScript compiler API provides a way to transpile source strings at runtime:

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);});
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
Saravana
  • 37,852
  • 18
  • 100
  • 108
  • Can you explain why `ts.transpile('const v: string = 123;')` doesn't produce any error? I would expect a typescript error... – Gershom Maes Jul 14 '23 at 17:50
8

Is it possible to save my Typescript code in a string and evaulate it during run time

Yes. By using the TypeScript compiler's transpile function.

More

Checkout TypeScript - Script : https://github.com/basarat/typescript-script which at it's core is simply ts.transpile : https://github.com/basarat/typescript-script/blob/163388be673a56128cc1e1b3c76588001a8c1b18/transpiler.js#L60

basarat
  • 261,912
  • 58
  • 460
  • 511
1

Had a headache resolving large JS chunk file when used with transpile.

But if there is a need to write a runtime transpiler please use the accepted answer. But for vanilla javascript in a typescript project, maybe the code below will help. Please checkout "Function".

With transpile:

import {transpile} from 'typescript'

const evaluatedResult = eval(ts.transpile("1+1"))
console.log(evaluatedResult) //2

Alternative:

const evaluatedResult = Function(`"use strict";return ${"1+1"}`)()
console.log(evaluatedResult) //2

Reasons:

  1. Just to use 'transpile', you will need to import the whole typescript. This cause the generated production file to be very large esp. when Webpack is used.
  2. It's that bit safer and faster than eval.
Han
  • 728
  • 5
  • 17
  • `Function` does not work with TypeScript; this does not answer the question. – kelsny Mar 14 '23 at 20:03
  • Thanks for correcting. My issue on understanding the question as I didn't see it's an Eval of a typescript. Anyway if anyone is looking for alternatives without typescript. The code will be. `const evaluatedResult = Function(return { Run: (data) => {console.log(data); return Promise.resolve("SUCCESS"); } }')(); evaluatedResult.Run("Hi").then((a:string) => console.log(a))` But for runtime in typescript go with the accepted answer (writing a transpiler) – Han Jun 05 '23 at 09:01