Generating typescript definitions?
See original GitHub issueHey @DanielCoulbourne,
I’m going to be working on automatically generating typescript definitions, the awesome thing about this is that for us typescript users we’ll be able to know the shape of the params object based off the route name and get alerted of any errors instantly. See below.
Anyway, my question is, would you merge this in, or should I work on creating my own package as an extension of Ziggy?
It would be an entirely optional secondary command e.g. you’d call
php artisan ziggy:generate "resources/routes.js"
php artisan ziggy:typescript "resources/types/routes.d.ts"
It would have no effect on the @routes
blade directive.
Cheers, Matt
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Documentation - Creating .d.ts Files from .js files - TypeScript
Run the TypeScript compiler to generate the corresponding d.ts files for JS files; (optional) Edit your package.json to reference the types. Adding TypeScript....
Read more >Generating TypeScript definition files directly from the source
Generating type definitions for a JavaScript module. For this we can use dts-gen which is a tool that generates definition files from any...
Read more >TypeScript Definition Generator 2019
To generate a .d.ts file, right-click any .cs or .vb file and select Generate TypeScript Definition. ... A .d.ts file is created and...
Read more >How to generate TypeScript definition file from any .js file?
In this article, we will see how to generate a TypeScript definition file from any .js file or any normal TypeScript file.
Read more >Generating TypeScript Definition Files from JavaScript
Generate TypeScript Definition Files for JSDoc Annotated JavaScript · Write your code in JS and apply JSDoc where needed · Use the forked ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry to dig up an old thread, I just hacked something together, it could maybe be created as a PR and merged in by somebody else 😃
I picked some pieces of current stuff and slapped them together in a way that works nicely.
First of all, yarn add @types/ziggy-js
Create a new laravel console command; I basically ripped the current ziggy:generate command and updated it to work with ts
Create a laravel mix extension to automatically generate the definitions when changing routes/compiling - also ripped the one that’s already documented
In webpack.mix.js
Now… when you build, this shims-ziggy.d.ts file will be generated with the following content:
Boom 😄
@iDevelopThings That’s amazing! Thank you!
A few minor adjustments to avoid some TS errors:
Use the routes type
[key: string]: { uri: string, methods: string[] };
which is more correct than[key: string]: string;
Remove
declare
from each item inside ofdeclare global
, which gives errorTS1038: A 'declare' modifier cannot be used in an already ambient context.
, it’s essentially redundant sincedeclare global
already saysdeclare
.Untab
{$routes}
by one step which makes it aligned properly in the output (avoids the double indentation)Tab in the heredoc (which is possible since PHP 7.3, see https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes)