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

Types not found when moduleResolution: node16 is set for typescript #177

Closed
sorgloomer opened this issue Aug 2, 2022 · 3 comments · Fixed by #184
Closed

Types not found when moduleResolution: node16 is set for typescript #177

sorgloomer opened this issue Aug 2, 2022 · 3 comments · Fixed by #184

Comments

@sorgloomer
Copy link

Describe the bug

Getting

main.ts:1:26 - error TS7016: Could not find a declaration file for module '@js-temporal/polyfill'. '.../node_modules/@js-temporal/polyfill/dist/index.cjs' implicitly has an 'any' type.

when trying to import in a project with moduleResolution: node16.

To Reproduce

package.json

{
  "dependencies": {
    "@js-temporal/polyfill": "^0.4.2",
    "typescript": "4.7.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "Node16",
    "noImplicitAny": true
  }
}

main.ts

import { Temporal } from '@js-temporal/polyfill';

and then run:

tsc

Output I get:

$ tsc
main.ts:1:26 - error TS7016: Could not find a declaration file for module '@js-temporal/polyfill'. '.../node_modules/@js-temporal/polyfill/dist/index.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/js-temporal__polyfill` if it exists or add a new declaration (.d.ts) file containing `declare module '@js-temporal/polyfill';`

1 import { Temporal } from '@js-temporal/polyfill';
                           ~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in main.ts:1

Additional context

My intent in this example is to import Temporal using require, and this is what's happening, but typescript doesn't seem to pick up the .d.ts file.

Typescripts esm support is not without controversies. One of them is that they are more strict about the location of .d.ts files: microsoft/TypeScript#49160 . The most straightforward solution seems to be moving and duplicating index.d.ts to dist/index.d.cts, dist/index.esm.d.ts and dist/index.umd.d.ts, but there might be better ways I am not aware of.

@12wrigja
Copy link
Contributor

12wrigja commented Aug 2, 2022

Based on telegraf/telegraf@6ddc397 I think we can do something very similar to #158, but also for the require import.

If you are familiar with how to repack npm bundles and use npm link, do you want to try this out and send a PR to fix?

It would be ideal if we could re-use the same index.d.ts file for all the references in exports.

@12wrigja
Copy link
Contributor

12wrigja commented Aug 2, 2022

import Temporal using require

Based on your repro, you aren't using CJS require, you are using ESM imports, no?

import { Temporal } from '@js-temporal/polyfill';

is ESM, not CJS?

@sorgloomer
Copy link
Author

Well, this is the tricky part. Although it looks like an esm static import, with these settings it gets transpiled to require by typescript if I am not mistaken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants