tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.91k stars 248 forks source link

feat: generate types that match generated js #692

Open rrmesquita opened 11 months ago

rrmesquita commented 11 months ago

Right now, the README already asks the user to add a piece of typing in a .d.ts file in order to make the IDE aware of the global route function. Since this function is global and always available, I don't see a reason to leave that step to the user.

Another problem that this PR solves, is the error message shown when you want to import the Ziggy config from the generated JS file into a TS file.

import { Ziggy } from './ziggy'
      // ˆ Module '"./ziggy"' has no exported member 'Ziggy'. (ts(2305))

I'll keep this as a draft for now, as this change would also require changes to tests and the README. Let me know your thoughts, and thanks for creating this awesome lib that helps me a lot!

bakerkretzmar commented 10 months ago

Very interested in making this work but need to think it through some more. We can't always count on being able to import from ziggy-js (see #680).

bakerkretzmar commented 10 months ago

Thinking out loud—could this be an option for the ziggy:generate command? Like we include this declaration by default but add something like --no-declare to let people omit it if they need to?

rrmesquita commented 10 months ago

If the --no-declare option is passed, it outputs the same as it does now, right? That could work I guess, but...

I see two types of people that would want these type definitions:

How problematic it would be to just require the user to install the npm package – which is super lightweight – at least as a dev dependency? If you think about, it makes sense, it fixes the issue, and it saves us from having to implement a not so elegant solution.

Let me know your thought and if there's another use-case I'm missing.

bakerkretzmar commented 10 months ago

Definitely not going to require installing the npm package, the goal is for almost everyone to never need it.