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

feat: generate types that match generated js #692

Draft
wants to merge 4 commits into
base: 2.x
Choose a base branch
from

Conversation

rrmesquita
Copy link

@rrmesquita rrmesquita commented Nov 30, 2023

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 bakerkretzmar self-assigned this Dec 1, 2023
@bakerkretzmar
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Author

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:

  • They have a build step, so chances are that this person already have the npm package installed.
  • They don't have a build step (maybe TALL?), uses the route helper injected by the @routes directive.

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
Copy link
Collaborator

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

@bakerkretzmar bakerkretzmar changed the base branch from main to 2.x February 20, 2024 21:52
@neochief
Copy link

neochief commented Nov 12, 2024

Hi!

From what I can figure out, the TS user of this package is required to set up their 'ziggy-js' dependency in some way anyway (either via tsconfig, or by having a NPM module). This is written in the TS section of the docs. So, I'm not sure what is the problem of importing from 'ziggy-js' in the TS context — it should be available in one way or the other. Besides, the current generated d.ts gives glaring TS error anyway.

I have tested the code suggested here, and it works fine. How about we proceed with this fix?

I should point out that the import in the suggested code should probably be changed to this one:

import { route as routeFn, type Config } from 'ziggy-js'

Related: #702

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

Successfully merging this pull request may close these issues.

3 participants