yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

Add type definitions to exports in package.json #21

Closed mrdrogdrog closed 1 year ago

mrdrogdrog commented 2 years ago

The package doesn't explicitly export the generated typescript definitions. This PR adds the type definition files to the exports in the package.json

I hope to solve this problem with that :sweat_smile:

src/y-doc-message-transporter.test.ts:11:27 - error TS7016: Could not find a declaration file for module 'y-protocols/awareness'. '[...]/node_modules/y-protocols/dist/awareness.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/y-protocols` if it exists or add a new declaration (.d.ts) file containing `declare module 'y-protocols/awareness';`
dmonad commented 2 years ago

Hi @mrdrogdrog,

thanks for the PRs! I didn't know that you can add a types field to named module exports.

Usually - or so I thought - typescript checks whether there is a .d.ts file next to the imported .js/.cjs file. This is why I currently export a module declaration for each exported file (there are definitely duplicates in dist/ and root /).

Can you please verify that this works by applying the changes from this PR to the package.json file in node_modules/y-protocols/package.json?

Could you also please tell me what kind of TS setup you are using (or TS version)? This is more for my own sanity. It might even motivate me to maintain the types field in the other packages.

mrdrogdrog commented 2 years ago

Good evening @dmonad,

thanks for the PRs!

Thanks for the lib :point_right: :sunglasses: :point_right:

I didn't know that you can add a types field to named module exports.

Neither did I until one of my projects exploded and I read the docs) for named exports.

Usually - or so I thought - typescript checks whether there is a .d.ts file next to the imported .js/.cjs file. This is why I currently export a module declaration for each exported file.

That's true usually typescript do so and it works fine. But the exploded project uses ts-jest which explictly wants to import the cjs version of the lib even though my project is a module. sigh anyway. ts-jest claims that the defintions are missing. So I checked the node_modules directory and......

(there are definitely duplicates in dist/ and root /)

.... found out that this isn't true :upside_down_face: . There are no declaration files in the dist folder. Or at least not in the package that I downloaded from npm. I guessed it was a mistake so I investigated this problem. Instead of fixing the build script and duplicate the .d.ts files I found this much more elegant solution.

Can you please verify that this works by applying the changes from this PR to the package.json file in node_modules/y-protocols/package.json?

It works. With this patch ts-jest found the declarations and did its job.

Could you also please tell me what kind of TS setup you are using (or TS version)? This is more for my own sanity. It might even motivate me to maintain the types field in the other packages.

I use the latest typescript version 4.7.4. Or what do you mean with setup? :thinking:

mrdrogdrog commented 2 years ago

Anything new?

mrdrogdrog commented 1 year ago

Hey. @dmonad I really need this change. Can you please take a look again? :/

dmonad commented 1 year ago

Sorry for the long wait. Will get this into a new release once #27 is ready