npm 导出typescript类时出现意外标识符

xpcnnkqh  于 2022-11-24  发布在  TypeScript
关注(0)|答案(1)|浏览(144)

我有一个名为Polynomial的typescript类,我想用它创建一个npm模块。为此,我试着用mocha测试它,用这个脚本:

//Polynomial.test.js
const Polynomial = require('./Polynomial.ts'); 
const assert = require('assert').strict;

describe("First test", function() {
    it("test", function() {
        let p = new Polynomial();
        assert(p.equals(new Polynomial("0")));
    });
});

当我执行npm test时,我在类的第一行得到错误Unexpected identifier,看起来像这样:

// Polynomial.ts
class Polynomial {
  private coefMap: Map<string, string> = new Map();  // <--- Error
  // ...
}
module.exports = Polynomial;

这就是package.json

{
  "name": "multivariate-polynomial",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "mocha Polynomial.test.js"
  }
}

tsconfig.json

{
    "compilerOptions": {
      "target": "es5",
      "lib": [
        "dom",
        "dom.iterable",
        "esnext"
      ],
      "module": "commonjs",
      "declaration": true,
      "outDir": "./lib",
      "strict": true
    },
  }

有什么问题吗?

8iwquhpp

8iwquhpp1#

更改libtarget并没有解决这个问题,但是我按照下面描述的步骤创建了一个新项目:
Setup a new TypeScript project with Mocha support
现在它起作用了。

  • 谢谢-谢谢

相关问题