Typescript 无法识别 d.ts 模块

标签 typescript

尝试在 Koa 中使用 typescript。
koa-respond@koa/cors没有@types包所以我添加了一个文件ambient-types.d.ts在我的 src 中带有这些声明

declare module 'koa-respond'
declare module '@koa/cors'

但是我仍然从 ts 得到这些错误
Try `npm install @types/koa__cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'koa__cors';`
src/server.ts(7,26): error TS7016: Could not find a declaration file for module 'koa-respond'. '/home/hayk/projects/learning/koa/koala/backend/node_modules/koa-respond/lib/koa-respond.js' implicitly has an 'any' type.
  Try `npm install @types/koa-respond` if it exists or add a new declaration (.d.ts) file containing `declare module 'koa-respond';`

这是我的tsconfig.json
{
"compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "noImplicitAny": true,
    "outDir": "./dist",
    "sourceMap": true
},
"include": [
    "./src/**/*"
]
}

我这样跑nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./index.ts

最佳答案

根据我的测试,您需要指定 --files ts-node 的选项强制它加载与 include 匹配的所有文件在您的 tsconfig.json 中设置, 否则 ambient-types.d.ts永远不会被加载。

nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node --files' ./index.ts

关于Typescript 无法识别 d.ts 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51837169/

相关文章:

angular - 动态添加组件 Angular 5

angularjs - 我如何在 typescript 构造函数之外正确使用 Angular $timeout 服务?

javascript - 错误 TS2339 : Property 'checked' does not exist on type 'HTMLElement'

angular - 在 Typescript 中扩展构建器

php - DarkaOnline Swagger-php 生成错误的界面

javascript - 如何使 v-btn 在点击时不启动路由但仍根据路由显示事件状态?

javascript - Webpack 将故事书添加到 create-react-app 时出错

使用 es6 而不是 Typescript 的 Angular cli

javascript - 如何从内容丰富的 CDA 响应中删除 sys 对象?

javascript - EventEmitter <string> 的上下文 'this' 未分配给 Observable <string> 类型的 'this' 方法