oven-sh/bun

Support TypeScript in the `Function` constructor, `eval` and `vm`

Opened this issue · 0 comments

What is the problem this feature would solve?

I searched https://github.com/oven-sh/bun/issues?q=is:issue+"function+constructor" and have found no existing issues related to this.

const fn = new Function(
  `
    const test: string = 'hi';
    return test;
  `
);

console.log(fn());
1 | const fn = new Function(
    ^
SyntaxError: Unexpected token ':'. const declared variable 'test' must have an initializer.
      at <parse> (:4:1)
      at Function (native:1:1)

This also applies to vm and eval where plain JavaScript works but not TypeScript.

It would solve being able to use TypeScript everywhere in Bun! We can write our code in TypeScript and build it or run it directly, but we can't run code at runtime while using TypeScript.

What is the feature you are proposing to solve the problem?

Hook up the transpiler Bun uses and exports via Bun.Transpiler to transpile the argument to the Function constructor before passing the transpiled JavaScript to the underlying JavaScript runtime.

What alternatives have you considered?

None.