如何在Typescript项目中使用节点16的定时器/承诺



在Node16版本中,我想要使用新的定时器/承诺API。我用NVM安装了Node 16,并切换到它:

$ nvm use       
Found '/Users/golergka/Projects/my-project/.nvmrc' with version <16>
Now using node v16.0.0 (npm v7.10.0)

甚至在我的package.json:中添加了引擎

"engines": { "node": ">=16" }

我不确定是否需要安装@types/node16版本,但我尝试了(没有成功(:

npm install --save-dev @types/node@16
npm ERR! code ETARGET
npm ERR! notarget No matching version found for @types/node@16.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

目前,我已经安装了^14.14.41

最后,我尝试在Typescript文件中导入setTimeout

import { setTimeout } from 'timers/promises'

但出现了一个错误:

Cannot find module 'timers/promises' or its corresponding type declarations.

我该怎么解决这个问题?

它已于3天前在@types/node中发布。只需运行npm install --save-dev @types/node@latestyarn add --dev @types/node@latest。版本为0。

当前节点类型已经过时,您只能用ts忽略来关闭错误

最新更新