尝试在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
答案 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