https://github.com/7nohe/laravel-typegen
The library lets you generate TypeScript types from your Laravel code
https://github.com/7nohe/laravel-typegen
inertia inertiajs laravel typegen typescript ziggy
Last synced: about 2 months ago
JSON representation
The library lets you generate TypeScript types from your Laravel code
- Host: GitHub
- URL: https://github.com/7nohe/laravel-typegen
- Owner: 7nohe
- Created: 2022-11-19T16:00:07.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-16T05:28:58.000Z (about 1 year ago)
- Last Synced: 2025-07-26T12:25:01.099Z (3 months ago)
- Topics: inertia, inertiajs, laravel, typegen, typescript, ziggy
- Language: TypeScript
- Homepage:
- Size: 296 KB
- Stars: 101
- Watchers: 3
- Forks: 5
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Laravel Typegen
## Table of contents
- [Features](#features)
- [Supported Versions](#supported-versions)
- [Installation](#installation)
- [Usage](#usage)
- [Enum Support](#enum-support)
- [Laravel Enum Support](#laravel-enum-support)
- [Use strongly typed `route()` function for ziggy](#use-strongly-typed-route-function-for-ziggy)
- [Useful types for Laravel](#useful-types-for-laravel)
- [Form Request types](#form-request-types)
- [Available Options](#available-options)
- [Development](#development)
- [Setup example project](#setup-example-project)
- [Debug](#debug)
- [License](#license)## Features
- Generate types from Laravel's Models
- Support Relationhips
- Support Enum (from PHP8.1)
- Generate types from Laravel's Form Requests
- Generate types for ziggy (Routing)
- Provide useful types for Laravel (e.g. pagination, etc.)## Supported Versions
This library supports the following versions:- Laravel 9.x and 10.x
- TypeScript 5.0 and above## Installation
```bash
$ npm install -D @7nohe/laravel-typegen
```Laravel Typegen may require installing `doctrine/dbal` via Composer in order to execute the `php artisan model:show` command internally.
```bash
$ composer require doctrine/dbal
```## Usage
Edit package.json
```json
{
"scripts": {
"typegen": "laravel-typegen"
},
}
``````bash
$ npm run typegen
```### Enum Support
We also support php8.1 enums.
```php
*/
protected $casts = [
'gender' => GenderType::class,
];
}
```This library will generate the following TypeScript types:
```typescript
export type User = {
id: number;
name: string;
email: string;
gender: GenderType;
email_verified_at?: string;
created_at?: string;
updated_at?: string;
posts?: Post[];
};
export enum GenderType {
Male = "Male",
Female = "Female",
Other = "Other"
}
```### Laravel Enum Support
If you use [Laravel Enum](https://github.com/BenSampo/laravel-enum), use the option `--laravel-enum`.```json
{
"scripts": {
"typegen": "laravel-typegen --laravel-enum"
},
}
```### Use strongly typed `route()` function for ziggy
Running the `laravel-typegen` command with the `--ziggy` option will generate route.d.ts.
It helps typing the `route()` function.
```json
{
"scripts": {
"typegen": "laravel-typegen --ziggy"
},
}
```For example, define the following routes
```php
// routes/web.php
Route::resource('posts', PostsController::class);
``````bash
$ php artisan route:list
GET|HEAD posts/{post} posts.show › PostsController@show
```Parameters will be checked strictly based on the route name.
```ts
// in your TypeScript code// OK
route('posts.show', { post: post.id })// Error
route('posts.show', { id: post.id })
```If you have created a project using the `--typescript` option of Laravel Breeze, you need to delete the declaration for the `route()` function in resources/js/types/global.d.ts.
```diff
import { PageProps as InertiaPageProps } from '@inertiajs/core';
import { AxiosInstance } from 'axios';
import ziggyRoute, { Config as ZiggyConfig } from 'ziggy-js';
import { PageProps as AppPageProps } from './';declare global {
interface Window {
axios: AxiosInstance;
}var route: typeof ziggyRoute;
var Ziggy: ZiggyConfig;
}- declare module 'vue' {
- interface ComponentCustomProperties {
- route: typeof ziggyRoute;
- }
- }declare module '@inertiajs/core' {
interface PageProps extends InertiaPageProps, AppPageProps {}
}
```### Useful types for Laravel
We provide useful types for Laravel (especially for Inertia).
For example, to return a paginated Inertia response in DashboardController, you can write the following
```php
paginate(5);
return Inertia::render(
'Dashboard',
[
'users' => $users
]
);
}
}```
You can import types already defined by Laravel Typegen.
```vue
import { Paginate } from '@7nohe/laravel-typegen';
import { User } from '@/types/model'; // generated typesdefineProps<{ users: Paginate<User> }>();
- - {{ user.name }}({{ user.email }})
```
### Form Request types
You can generate types from Laravel's Form Request by executing the command with the `--form-requests` option.If you have the Form Request class as shown below:
```php
*/
public function rules()
{
return [
'name' => ['string', 'max:255'],
'email' => ['email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
];
}
}
```The corresponding TypeScript types will be automatically generated as follows:
```ts
// formRequests.ts
export type ProfileUpdateRequest = {
name?: string;
email?: string;
};
```By using these generated types in combination with HTTP Client libraries like Axios, you can write more type-safe code.
## Available options
```bash
Usage: laravel-typegen [options]Generate TypeScript types from your Laravel code
Options:
-V, --version output the version number
-o, --output Output directory (default: "resources/js/types")
--laravel-enum Use Laravel Enum (default: false)
--enum-path Path to enum files (default: "app/Enums")
--model-path Path to model files (default: "app/Models")
-z, --ziggy Generate types for ziggy (default: false)
--vendor-routes Include routes defined by vendor packages (default: false)
--ignore-route-dts Ignore generating route.d.ts (default: false)
--form-request Generate types for FormRequests (default: false)
--form-request-path Path to FormRequest files (default: "app/Http/Requests")
-h, --help display help for command
```## Development
### Setup example project
```bash
$ cd examples/laravel10-app
$ cp .env.example .env
$ docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$(pwd):/var/www/html" \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
$ ./vendor/bin/sail up -d
$ ./vendor/bin/sail php artisan key:generate
$ ./vendor/bin/sail php artisan migrate --seed
$ ./vendor/bin/sail npm install
```### Debug
```bash
$ pnpm install
$ sh debug.sh
```## License
MIT