5

Trying to use typescript with Koa.

koa-respond and @koa/cors do not have @types packages so I added a file ambient-types.d.ts in my src with these declarations

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

However I still get these errors from 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';`

This is my tsconfig.json

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "noImplicitAny": true,
    "outDir": "./dist",
    "sourceMap": true
},
"include": [
    "./src/**/*"
]
}

And I run like this nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./index.ts

2 Answers 2

14

According to my testing, you need to specify the --files option to ts-node to force it to load all the files matching the include setting in your tsconfig.json, otherwise ambient-types.d.ts never gets loaded.

nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node --files' ./index.ts
Sign up to request clarification or add additional context in comments.

Comments

-2

you might need to specify typeRoots directory in tsconfig.json in that directory, I usually create subdirectories with same names as the modules I want to writing typings for each directory would have a index.d.ts file, with declare module ...... { ... } stuff in it

/ 
/tsconfig.json
/typings
/typings/my-module/index.d.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.