Could not find the declaration file of my Typescript library even when it exists

1.4k views Asked by At

I have the exact same problem mentioned in this other question, but unfortunately I was not able to solve it in the same way.

Basically I wrote a Typescript library and now, before publishing it, I would like to test it locally and check if it gets imported correctly.

This is my test project tree:

.
├── js-src
│   └── index.js
├── node_modules
│   └── response-giver -> ../../response-giver
├── package-lock.json
├── tsconfig.json
└── ts-src
    └── index.ts

You can see my module directly on my repo: https://github.com/Giovarco/response-given/tree/develop

This is how I tried to change the tsconfig.json on my module to solve the problem:

{
    "compilerOptions": {
        "emitDecoratorMetadata" : true,
        "experimentalDecorators" : true,
        "module" : "commonjs",
        "target" : "ES5",
        "watch" : true,
        "outDir" : "js-src",
        "rootDir" : "ts-src",
        "declaration": true,
        "allowJs": false,
        "lib": [ "es2015" ],
        "typeRoots": [
            "node_modules/@types",
            "js-src/index"
        ],
        "main" : "js-src/index"

    }
}

This is how I am trying to import my module:

import * as responseGiver from "response-giver";

but I get this error:

[ts]
Could not find a declaration file for module 'response-giver'. '/home/mario/Git projects/response-giver/js-src/index.js' implicitly has an 'any' type.
  Try `npm install @types/response-giver` if it exists or add a new declaration (.d.ts) file containing `declare module 'response-giver';`

This is what works nonetheless:

import * as responseGiver from "../node_modules/response-giver";

but I don't get the right signature for my functions when I try to use them.

2

There are 2 answers

0
Giovarco On BEST ANSWER

This is how I solved the problem. Basically add "types": "js-src/index.d.ts" to the package.json.

3
NN_ On

Do you have node_modules/@types/response-giver ?