tighten / ziggy

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

Add type declarations and `ziggy.d.ts` generation #664

Closed bakerkretzmar closed 1 year ago

bakerkretzmar commented 1 year ago

This PR does two things: it adds types for Ziggy itself, and it adds the ability to generate type declarations specific to a user's list of routes. Credit for most of this goes to @lmeysel for the original implementation in #655.

Types

This PR adds an index.d.ts file containing type definitions for Ziggy itself. These types will be maintained manually since Ziggy is written in JavaScript. Out of the box they should be able to replace @types/ziggy-js and provide much smoother autocompletion and type hinting for end users.

They're currently tested manually using tests/js/types.ts, which contains sample Ziggy code that should or shouldn't produce type errors.

User route types

This PR also adds --types and --types-only options to the ziggy:generate command to generate a ziggy.d.ts file containing additional types specific to the app using Ziggy. This file extends Ziggy's RouteList interface to list the names and some basic parameter metadata about all the routes in the app, which allows IDE autocompletion of routes and parameter names while using the route() function.

bakerkretzmar commented 1 year ago

No idea what's going on with the line endings... the .js files work fine.