Typescript无法识别d.ts模块

时间:2018-08-14 08:38:22

标签: typescript

尝试在Koa中使用打字稿。

koa-respond@koa/cors没有@types软件包,因此我在src中添加了带有这些声明的文件ambient-types.d.ts

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您可能需要在tsconfig.json中指定typeRoots目录 在该目录中,我通常会创建与要为其键入内容的模块相同名称的子目录。 每个目录中都有一个index.d.ts文件,其中包含declare module ...... { ... }个文件

/ 
/tsconfig.json
/typings
/typings/my-module/index.d.ts
相关问题