Mocha with TypeScript: Parameter 'id' impl

2019-09-02 19:41发布

I wrote a simple function as follows:

export const courseIndustry: any = {
  getById: (id: string) => {
    return {};
  }
};

And I thought I specified that the argument id is of type string, but the mocha test claims:

mocha -r ts-node/register -r espower-typescript/guess test/**/*.test.ts

Unable to compile TypeScript: courseIndustry.ts(4, 15): error TS7006:  Parameter 'id' implicitly has an 'any' type.

Where am I wrong?

2条回答
Rolldiameter
2楼-- · 2019-09-02 20:02

Try to use a another transpiler like typescript-compiler or any other and try to complie.

enter link description here

查看更多
姐就是有狂的资本
3楼-- · 2019-09-02 20:09

The error seems thrown by espower-typescript/guess. So I checked its document and find the following switch can work around the errors: 'TS_NODE_TRANSPILE_ONLY=1'.

So the final version of the shell command make it work:

TS_NODE_TRANSPILE_ONLY=1 mocha -r ts-node/register --require espower-typescript/guess test/**/*.test.ts --exit
查看更多
登录 后发表回答