jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.2k stars 505 forks source link

Wrong type file (d.ts) generated #1108

Open ImanMh opened 2 years ago

ImanMh commented 2 years ago

Current Behavior

Minimal example: https://github.com/ImanMh/ts-import-types-issue You can find 2 commits. The first one is the initialization commit and it works fine. Ignore that. The second one is an example where consumers of this package can import the two function (sum and concat) and happily use it, but the types don't work due to issues in the generated d.ts file.

These are 2 separate problems but I guessed maybe the are being originated from the same source. You can link this package locally and see yourself that the types are not working. Here is an output of the generated type file:

import { sum } from 'math/sum';
import { concat } from 'string/concat';
export { sum, concat };

Expected behavior

If I want to make this work in other consumer projects, I should change the type file like this:

import { sum } from './math/sum/sum';
import { concat } from './string/concat';
export { sum, concat };

Then it works perfectly. The issue with the ./ at the beginning of files appear when you use "baseUrl": "./src" in your tsconfig. If I want to remove it then I'll end up with ugly paths everywhere.

The issue with './math/sum' appears when I add a package.json file inside sum directory. here is the content of the package.json file:

{
  "main": "sum.ts"
}

In the output file this package.json file is removed so there is no way for the compiler to know where to find the sum.ts .

Suggested solution(s)

consider ./ at the beginning of the output index.d.ts and concat package.json.main to the end of the paths of any directory.

Additional context

I hope I'm not missing something obvious. I know that this issue might be related to TS itself and not TSDX but I just wanted to make sure and discuss it with you.

Your environment

  System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 114.29 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.8.0 - ~/.nvm/versions/node/v16.8.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.8.0/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v16.8.0/bin/npm
  Browsers:
    Chrome: 97.0.4692.71
    Firefox: 96.0.1
    Safari: 14.1.2
    Safari Technology Preview: 15.4
  npmPackages:
    tsdx: ^0.14.1 => 0.14.1
    typescript: ^4.5.5 => 4.5.5