关键字 'private' 在解析 typescript 代码为 ast 时被保留

The keyword 'private' is reserved when I parse typescript code into ast

我想将这段代码解析为 ast:

const code = `
 class Test {
  constructor(private foo: Foo) { }
 }
`;

但是当我 运行 我得到错误:

  The keyword 'private' is reserved

这是完整代码:

import * as escodegen from "escodegen";
import { Parser } from "acorn";

const code = `
class Test {
  
  constructor(private foo: Foo) { }
}
`;

const acorn = Parser.extend(require("acorn-jsx")(), require("acorn-bigint"));

const ast = acorn.parse(code, { ranges: false, ecmaVersion: "latest" });

const output = escodegen.generate(ast);

console.log({ output });

astexplorer.net it is works. How make it work in codesandbox.io

如果您想使用打字稿代码,您需要使用打字稿编译器。 您可以在这里阅读更多内容:https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API