tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.83k stars 247 forks source link

Export the types file #721

Closed simon-tma closed 5 months ago

simon-tma commented 5 months ago

If there is an exports key set, Typescript requires the types file to be included in the list of the files exported.

Without this change, Typescript errors when trying to import types with:

error TS7016: Could not find a declaration file for module 'ziggy-js'. '/.../node_modules/ziggy-js/dist/index.js' implicitly has an 'any' type.
  There are types at '/.../node_modules/ziggy-js/src/js/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'ziggy-js' library may need to update its package.json or typings.
Mohammad-Alavi commented 5 months ago

First of all thanks for this great package ❤️ . I really mean it. Also, your docs are the worst I have ever seen ❤️ (sorry, just wanted to get it out of chest!)

Please merge this one. This is really a high priority fix.

bakerkretzmar commented 5 months ago

@simon-tma what version of TypeScript are you using? Can you link me to where TypeScript says types is required (I can see that it's available, but it looks optional). Happy to add this if it's necessary but the TS docs make it seem like everything should work fine either way.

simon-tma commented 5 months ago

@bakerkretzmar After further digging (using https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution, https://github.com/microsoft/TypeScript/issues/52363 and https://arethetypeswrong.github.io/?p=ziggy-js%402.0.4), it's not that types is required in exports, it's that the top-level one is ignored when doing resolution using exports so typescript falls back to looking for sibling files instead.

Resolution trace from "Are The Types Wrong" showing what paths are being searched for the declaration file:

Screenshot 2024-03-20 at 08 57 25

I'm seeing this with TS 5.3 and 5.4, though it's likely to happen with any version of Typescript using nodenext or node16 for module resolution.

bakerkretzmar commented 5 months ago

@simon-tma gotcha, makes sense. Appreciate you sharing those details, and that "are the types wrong" tool is very cool.

bakerkretzmar commented 5 months ago

Released just now in v2.0.5. Thanks again for digging into this 🙏🏻