Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No .d.ts Declaration Files for JS Files Are Emitted (allowJs: true) #1260

Closed
hediet opened this issue Mar 7, 2021 · 4 comments · Fixed by #1483
Closed

No .d.ts Declaration Files for JS Files Are Emitted (allowJs: true) #1260

hediet opened this issue Mar 7, 2021 · 4 comments · Fixed by #1483

Comments

@hediet
Copy link
Contributor

hediet commented Mar 7, 2021

When setting allowJs: true in tsconfig.json, typescript files can import javascript files.
Typescript is also clever about the types used in javascript and infers as much as possible.
Such inferred javascript types might even be exported by typescript files and become part of the library API.

Expected Behaviour

ts-loader should emit declaration files for javascript files when allowJs is set to true and ts-loader is configured to handle javascript files.

Actual Behaviour

ts-loader does not emit declaration files for javascript files that are imported in typescript files. This results in broken declaration files.

Steps to Reproduce the Problem

  1. clone https://github.com/hediet/webpack-allowjs-declaration-issue-repro
  2. run yarn
  3. run yarn build
  4. Notice this error (no declaration files for javascript):

image

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/hediet/webpack-allowjs-declaration-issue-repro

Btw., thanks for creating and maintaining ts-loader!

@hediet
Copy link
Contributor Author

hediet commented Mar 7, 2021

I guess this should be fixed by changing this line of code:

if (fileName.match(constants.tsTsxRegex) === null) {

into this, as done in this line:

if (fileName.match(compilerOptions.allowJs === true
      ? constants.dtsTsTsxJsJsxRegex
      : constants.dtsTsTsxRegex;) === null) {

Edit: I can confirm that this fixes this issues. Idk whether it causes other issues.

@appzuka
Copy link
Member

appzuka commented Mar 10, 2021

I looked at your repo and confirmed what you reported. I can also confirm that when using tsc and allowJs the declaration files are generated, so I don't see any reason why ts-loader should not do this. Perhaps you would like to submit a PR with your proposed fix.

@hediet
Copy link
Contributor Author

hediet commented Mar 12, 2021

I submitted a PR!

@as-zlynn-philipps
Copy link

one... year... later...

johnnyreilly added a commit that referenced this issue Jun 22, 2022
)

* Fixes #1260 by generating declaration files for js files if allowJs is set to true.

* Fix declarationOutputAllowJs test

Update expected test output for 4.7 and
stabilize expectation to be OS independent.

* Update package.json and CHANGELOG.md for 9.3.1

* Update CHANGELOG.md

Co-authored-by: John Reilly <johnny_reilly@hotmail.com>

Co-authored-by: Henning Dieterichs <henning.dieterichs@live.de>
Co-authored-by: John Reilly <johnny_reilly@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants