Search code examples
javascripttypescriptidewebstormrollup

How to set sourse map for d.ts files?


I've created a TypeScript library. It has an src directory with .ts files and dist/esm directory, where all .ts file are converted into .js, .js.map and .d.ts files. The library is working, it has typings but there are some problems with the development process.

In the IDE (I use Webstorm) when I click to the component from my library I'd like to open a source file from src directory. But instead I open a typings declaration file (.d.ts).

I've found out that when I add to the .d.ts file a link to a source map like so:

/// <reference types="react" />
import './styles/Button.scss';
export declare const Button: () => JSX.Element;
// The line below has been added
//# sourceMappingURL=Button.js.map

the IDE starts automatically open needed file from src directory.

And I'd like to know is it a correct way to open files from source directory of a library?

If it is, how can I automatically add these lines in every .d.ts files using Rollup?

If it's not, could you please describe how to do it in the correct way?


Solution

  • Suddenly I found out that there's a special field in tsconfig.json:

    {
      "compilerOptions": {
        // ...
        declarationMap: true
      }
    }
    

    And now it works.