js-temporal/temporal-polyfill

Types not found when `moduleResolution: node16` is set for typescript

sorgloomer opened this issue · 3 comments

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.

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.

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?

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.