打字稿无法识别 d.ts 模块

Hay*_*yan 4 typescript

尝试在 Koa 中使用打字稿。

koa-respond并且@koa/cors没有@types包,所以我ambient-types.d.ts在我的 src 中添加了一个带有这些声明的文件

declare module 'koa-respond'
declare module '@koa/cors'
Run Code Online (Sandbox Code Playgroud)

但是我仍然从 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';`
Run Code Online (Sandbox Code Playgroud)

这是我的 tsconfig.json

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "noImplicitAny": true,
    "outDir": "./dist",
    "sourceMap": true
},
"include": [
    "./src/**/*"
]
}
Run Code Online (Sandbox Code Playgroud)

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

Mat*_*hen 12

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

nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node --files' ./index.ts
Run Code Online (Sandbox Code Playgroud)