alexjoverm / typescript-library-starter

Starter kit with zero-config for building a library in TypeScript, featuring RollupJS, Jest, Prettier, TSLint, Semantic Release, and more!
MIT License
4.37k stars 493 forks source link

Could not find a declaration file for module #274

Closed anasanzari closed 2 years ago

anasanzari commented 5 years ago

Typescript complains when you try to import modular lib, as mentioned in the readme

import something from 'mylib/dist/lib/something'

iplus26 commented 5 years ago

solution for this: just copy all *.d.ts files from dist/types to dist/lib, so that all js modules in dist/lib will get its own definition.

To take a step forward, I changed declarationDir and outDir in my tsconfig file, for example, dist/lib or just lib.

{
    "compilerOptions": {
        // ...
        "declarationDir": "lib",
        "outDir": "lib",
    }
}

After that, users of my lib can import as they like:

import { something } from 'mylib'

// equals 

import { something } from 'mylib/lib/something'

@alexjoverm Do u have any inside thoughts? Should this be the default settings for typescript-library-starter? I'm willing to provide a pr if having you permission.

ryancat commented 5 years ago

There are two ways to use the file in dist folder. One is to use it as dependencies from 'node_modules', in that case rollup or webpack will look at your package.json file and find the typing directory under types or typings. In that case, your declaration files don't need to be with the source code. The other way is to use it like you said as direct import from dist folder. In this case, typescript compiler has no way to figure out where is the declaration file for your module. It will first try to find an index.d.ts file, then the file with the same name to your module, if they both failed, it will throw exception on Could not find a declaration file for module.

What @iplus26 mentioned above will work as it moves the declaration file next to the module source code. Another way is to set your typeRoot in tsconfig.json to the lib folder, so that tsc will always find it correctly.