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

Typescript Set transpilation issues #530

Open
RobPruzan opened this issue Jun 20, 2023 · 0 comments
Open

Typescript Set transpilation issues #530

RobPruzan opened this issue Jun 20, 2023 · 0 comments

Comments

@RobPruzan
Copy link

RobPruzan commented Jun 20, 2023

When transpiling typescript that has Set usage, many operations do not work as expected. Works correctly when using conventional JS syntax.
Example:

  var jsCode = `
  var visited = new Set();
  var visualization = [];
  visited.add(1);
  visited.add(2);
  console.log(visited, [...visited.keys()]);
  visited
  //... your other code ...
`;

  const vm = new VM({
    timeout: 10000,
    sandbox: {
      globalVar,
      console: {
        log: (...args) => {
          console.log('From sandbox:', ...args);
        },
      },
    },
  });

Output:

From sandbox: Set {} [ 1, 2 ]

When attempting to run transpiled typescript (ts is identical to js code in prev example) with the same operation:

  const jsCode = ts.transpileModule(code, {
    compilerOptions: { module: ts.ModuleKind.CommonJS },
  }).outputText;
  console.log('the js code', jsCode);

  const vm = new VM({
    timeout: 10000,
    sandbox: {
      globalVar,
      console: {
        log: (...args) => {
          console.log('From sandbox:', ...args);
        },
      },
    },
  });

Transpilled JS output:

code var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var visited = new Set();
var visualization = [];
visited.add(1);
visited.add(2);
console.log(visited, __spreadArray([], visited.keys(), true));
visited

Program output:

From sandbox: Set {} []

Normal set operations work like .size or has, but there is no way to access the items in the Set other then an operation like:

var visited = new Set();
var visualization = [];
visited.add(1);
visited.add(2);
console.log(visited, visited.keys().next().value);
visited

Transpiled TS

code var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var visited = new Set();
var visualization = [];
visited.add(1);
visited.add(2);
console.log(visited, __spreadArray([], visited.keys(), true));
visited;

Output of program:

From sandbox: Set {} 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant