'work with d.ts with a different folder for js (pdfjs-dist)

Currently pdfjs-dist https://github.com/mozilla/pdfjs-dist/tree/bd2a6cd87f9e0c3a9b3197d5d27acfb99886629b contains the d.ts and js files in separate folders:

https://github.com/mozilla/pdfjs-dist/blob/bd2a6cd87f9e0c3a9b3197d5d27acfb99886629b/types/src/display/node_utils.d.ts

https://github.com/mozilla/pdfjs-dist/blob/bd2a6cd87f9e0c3a9b3197d5d27acfb99886629b/lib/display/node_utils.js

If i import them with:

import { NodeCanvasFactory } from 'pdfjs-dist/types/src/display/node_utils';

it generates wrong js code:

const api_1 = require('pdfjs-dist/types/src/display/api');

Is it possible to properly map the d.ts along with js?

The result should be the following:

const api_1 = require("pdfjs-dist/lib/display/api");

I also tried to work with tsconfig.json

"paths": {
  "pdfjs-dist/types/src/*": ["./pdfjs-dist/lib/*"]
}

Full tsconfig.json

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        "lib": [
            "es2017",
            "dom"
        ],
        "outDir": "dist",
        "rootDir": "src",
        "strict": true,
        "strictNullChecks": true,
        "noImplicitAny": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "forceConsistentCasingInFileNames": true,
        "declaration": true,
        "types": [
            "node",
            "jest"
        ],
        "sourceMap": true,
        "paths": {
            "pdfjs-dist/types/src/*": ["./pdfjs-dist/lib/*"]
        }
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules", 
        "**/*.spec.ts"
    ]
}


Solution 1:[1]

Here is the solution I came up with for the same problem. (In your question you were mixing api and node_utils, but will answer assuming you want to import node_utils).

First, create a declaration file named node_utils.d.ts (Note that the name of the file has to be the same as the module name).

In this file add the following:

declare module 'pdfjs-dist/lib/display/node_utils' {
    export * from 'pdfjs-dist/types/src/display/node_utils'
}

Now you can use pdfjs-dist/lib/display/node_utils when importing like below and Typescript will be pick up the declaration files. Unfortunately, this has to be done for every file you want to import that has typing in separate folders from the js files!

import { NodeCanvasFactory } from 'pdfjs-dist/lib/display/node_utils';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1