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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,6 @@ To generate route types, run the `ziggy:generate` command with the `--types` or
php artisan ziggy:generate --types
```

To make your IDE aware that Ziggy's `route()` helper is available globally, and to type it correctly, add a declaration like this in a `.d.ts` file somewhere in your project:

```ts
import routeFn from 'ziggy-js';

declare global {
var route: typeof routeFn;
}
```

If you don't have Ziggy's NPM package installed, add the following to your `jsconfig.json` or `tsconfig.json` to load Ziggy's types from your vendor directory:

```json
Expand Down
6 changes: 5 additions & 1 deletion src/Output/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ public function __toString(): string

return <<<JAVASCRIPT
/* This file is generated by Ziggy. */
import routeFn, { type Config } from 'ziggy-js'
declare module 'ziggy-js' {
interface RouteList {$routes->toJson(JSON_PRETTY_PRINT)}
}
export {};
declare global {
var route: typeof routeFn
}
export declare const Ziggy: Config;

JAVASCRIPT;
}
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/ziggy-7.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This file is generated by Ziggy. */
import routeFn, { type Config } from 'ziggy-js'
declare module 'ziggy-js' {
interface RouteList {
"posts.index": [],
Expand All @@ -18,4 +19,7 @@ declare module 'ziggy-js' {
]
}
}
export {};
declare global {
var route: typeof routeFn
}
export declare const Ziggy: Config;
6 changes: 5 additions & 1 deletion tests/fixtures/ziggy.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This file is generated by Ziggy. */
import routeFn, { type Config } from 'ziggy-js'

Check failure on line 2 in tests/fixtures/ziggy.d.ts

View workflow job for this annotation

GitHub Actions / Ubuntu, PHP 7.4, Laravel 5.6.*

Cannot find module 'ziggy-js' or its corresponding type declarations.

Check failure on line 2 in tests/fixtures/ziggy.d.ts

View workflow job for this annotation

GitHub Actions / Ubuntu, PHP 7.4, Laravel 5.5.*

Cannot find module 'ziggy-js' or its corresponding type declarations.

Check failure on line 2 in tests/fixtures/ziggy.d.ts

View workflow job for this annotation

GitHub Actions / Ubuntu, PHP 8.1, Laravel 8.*

Cannot find module 'ziggy-js' or its corresponding type declarations.

Check failure on line 2 in tests/fixtures/ziggy.d.ts

View workflow job for this annotation

GitHub Actions / Ubuntu, PHP 7.4, Laravel 8.*

Cannot find module 'ziggy-js' or its corresponding type declarations.

Check failure on line 2 in tests/fixtures/ziggy.d.ts

View workflow job for this annotation

GitHub Actions / Ubuntu, PHP 7.3, Laravel 5.8.*

Cannot find module 'ziggy-js' or its corresponding type declarations.
declare module 'ziggy-js' {
interface RouteList {
"posts.index": [],
Expand All @@ -9,4 +10,7 @@
]
}
}
export {};
declare global {
var route: typeof routeFn
}
export declare const Ziggy: Config;
Loading