Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeScript. #323

Closed
wintertime-inc opened this issue Oct 12, 2020 · 5 comments
Closed

Support TypeScript. #323

wintertime-inc opened this issue Oct 12, 2020 · 5 comments
Labels

Comments

@wintertime-inc
Copy link

Hi there, any plans to support typescript?
Or maybe there is someone who uses the custom ts compiler in production, if so would you like to share?

@theoparis
Copy link

theoparis commented Oct 16, 2020

I would definitely like to see this, as I was trying to make a discord bot that executes typescript code but I couldn't find another typescript execution library that supports sandboxing and this is probably the best out there.
EDIT: it seems like this library uses https://nodejs.org/api/vm.html behind the scenes, which doesn't seem to support typescript either.

@y21
Copy link
Contributor

y21 commented Oct 16, 2020

Have you tried using the TypeScript Compiler API? It provides methods to transform TypeScript code into JavaScript code dynamically. I've used it before and it works great.

const {VM} = require('vm2');
const ts = require('typescript');

const vm = new VM();

const res = ts.transpile(`
    let x: number = 3;
    x + 5;
`);

console.log(vm.run(res)); // 8

@theoparis
Copy link

Oh, for some reason I didn't think of using it in combination with vm2 lol. I will try this, thanks.

@stale
Copy link

stale bot commented Jan 17, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 17, 2021
@XmiliaH
Copy link
Collaborator

XmiliaH commented Apr 8, 2021

It should also be possible to set ts.transpile as compiler.

const {VM} = require('vm2');
const ts = require('typescript');

const vm = new VM({compiler: ts.transpile});

console.log(vm.run(`
    let x: number = 3;
    x + 5;
`)); // 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants