如何修复节点类型的 tsc 抛出错误?

How to fix tsc throwing errors for node typings?

我已经使用打字稿开始了一个新的节点项目。

我通过以下方式安装了打字稿:

$ npm install typescript@2 -g

我的环境是这样的:

$ tsc -v
Version 2.0.0

$ node -v
v7.10.0

我通过以下方式安装了节点类型:

npm install --save @types/node

我预计:

tsc

刚刚 运行 顺利通过。相反,我收到有关打字本身的错误:

366         isTTY?: true;
                    ~~~~

node_modules/@types/node/index.d.ts(366,17): error TS1110: Type expected.


1907         all?: false;
                   ~~~~~

node_modules/@types/node/index.d.ts(1907,15): error TS1110: Type expected.


1911         all: true;
                  ~~~~

node_modules/@types/node/index.d.ts(1911,14): error TS1110: Type expected.


1930         ttl: true;
                  ~~~~

node_modules/@types/node/index.d.ts(1930,14): error TS1110: Type expected.


4138     type DoesZapCodeSpaceFlag = 0 | 1;
                                     ~

node_modules/@types/node/index.d.ts(4138,33): error TS1110: Type expected.

我的打字稿无论如何都会转译,但我宁愿修复这些错误。如何修复它们?

我使用的是过时的 tsc 版本。您可以通过以下方式检查这是否是一个问题:

$ which tsc
/usr/local/bin/tsc

它没有被 "npm install typescript -g" 更新,因为它们安装到相对于我的 nvm 的路径。

相反,我现在通过以下方式为项目使用本地版本的打字稿:

npm install typescript --save

这会将可执行文件存储到:

$ ./node_modules/typescript/bin/tsc -v
Version 2.3.2

npm scripts 默认查找本地二进制文件,因此我在 package.json:

中添加了脚本
"scripts": {
    "console": "tsc && node dist/console.js",
},

所以我可以 运行 只是:

npm run console