PHPackages                             synergitech/laravel-typescript-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. synergitech/laravel-typescript-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

synergitech/laravel-typescript-generator
========================================

Generate TypeScript type definitions from Laravel Eloquent models via schema introspection and model metadata.

1.0.0(3mo ago)17MITPHPPHP ^8.1CI passing

Since Apr 21Pushed 3mo agoCompare

[ Source](https://github.com/SynergiTech/laravel-typescript-generator)[ Packagist](https://packagist.org/packages/synergitech/laravel-typescript-generator)[ RSS](/packages/synergitech-laravel-typescript-generator/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (7)Versions (2)Used By (0)

Laravel TypeScript Generator
============================

[](#laravel-typescript-generator)

[![Tests](https://github.com/synergitech/laravel-typescript-generator/actions/workflows/tests.yml/badge.svg)](https://github.com/synergitech/laravel-typescript-generator/actions/workflows/tests.yml)

Generate TypeScript type definitions from your Laravel Eloquent models automatically. Uses **both** database schema introspection **and** model metadata (`$casts`, `$timestamps`, etc.) to produce accurate types.

Version Support
---------------

[](#version-support)

LaravelPHPSupported13.x^8.3✓12.x^8.2✓11.x^8.2✓10.x^8.1✓Features
--------

[](#features)

- **Schema introspection** — reads your actual database columns, types, and nullability
- **Cast-aware** — Laravel `$casts` override the raw DB type (e.g. a `json` column cast to `array` becomes `unknown[]`)
- **Relationship support** — optionally include `hasMany`, `belongsTo`, etc. as typed properties
- **API Resource support** — optionally generate types from `JsonResource` classes (what your API actually returns)
- **Per-model files** — generates one `.d.ts` per model plus a barrel `index.d.ts`
- **Configurable** — nullable style (`| null` vs `?`), excluded models, manual type overrides

---

Installation
------------

[](#installation)

```
composer require synergitech/laravel-typescript-generator
```

The service provider is auto-discovered by Laravel. No manual registration needed.

### Publish the config (optional)

[](#publish-the-config-optional)

```
php artisan vendor:publish --tag=typescript-generator-config
```

This creates `config/typescript-generator.php` where you can customise everything.

---

Usage
-----

[](#usage)

### Basic generation

[](#basic-generation)

```
php artisan types:generate
```

This scans `App\Models`, introspects each model's database table, and writes `.d.ts` files to `resources/js/types/`.

### With relationships

[](#with-relationships)

```
php artisan types:generate --with-relationships
```

### With API resources

[](#with-api-resources)

```
php artisan types:generate --with-resources
```

Resources are off by default. Pass `--with-resources` (or set `include_resources: true` in config) to scan `App\Http\Resources` and generate a `.d.ts` per resource in `resources/js/types/resources/`.

### Dry run (preview without writing)

[](#dry-run-preview-without-writing)

```
php artisan types:generate --dry-run
```

---

Example Output
--------------

[](#example-output)

Given a `User` model with this table:

ColumnTypeNullableidbigintnonamevarchar(255)noemailvarchar(255)nois\_adminbooleannometadatajsonyescreated\_attimestampyesupdated\_attimestampyesAnd these casts:

```
protected $casts = [
    'is_admin' => 'boolean',
    'metadata' => 'array',
];
```

The generator produces `resources/js/types/User.d.ts`:

```
// Auto-generated by laravel-typescript-generator
// Model: App\Models\User
// Generated at: 2026-04-08T12:00:00+00:00

export interface User {
  /** DB: bigint */
  id: number;
  /** DB: varchar | Cast: string */
  name: string;
  /** DB: varchar | Cast: string */
  email: string;
  /** DB: boolean | Cast: boolean */
  is_admin: boolean;
  /** DB: json | Cast: array | nullable */
  metadata: unknown[] | null;
  /** DB: timestamp | nullable */
  created_at: string | null;
  /** DB: timestamp | nullable */
  updated_at: string | null;
}
```

### Resource output

[](#resource-output)

Given a `UserResource` that returns:

```
public function toArray(Request $request): array
{
    return [
        'id'         => $this->id,
        'name'       => $this->name,
        'email'      => $this->email,
        'role'       => $this->when($request->user()?->isAdmin(), $this->role),
        'address'    => new AddressResource($this->address),
    ];
}
```

The generator produces `resources/js/types/resources/UserResource.d.ts`:

```
// Auto-generated by laravel-typescript-generator
// Resource: App\Http\Resources\UserResource

import type { AddressResource } from './AddressResource';

export interface UserResource {
  id: unknown | null;
  name: unknown | null;
  email: unknown | null;
  role?: unknown;
  address: AddressResource;
}
```

Fields from `$this->when(...)` whose condition is false at generation time are marked optional (`?`). Fields whose runtime value is `null` (empty model attributes) are typed `unknown | null`. You can refine these with `type_overrides` in config if needed.

With `--with-relationships`, if User `hasMany` Post:

```
export interface User {
  // ... attributes ...

  // Relationships
  posts?: Post[];
}
```

---

Configuration
-------------

[](#configuration)

KeyDefaultDescription`model_namespace``App\Models`Namespace to scan for models`model_directory``app/Models`Directory path corresponding to the namespace`output_directory``resources/js/types/models`Where model `.d.ts` files are written`include_relationships``false`Include relationships by default (overridden by CLI flags)`include_timestamps``false`Force-include timestamp columns even if model has `$timestamps = false``nullable_style``union``'union'` → `type | null`, `'optional'` → `type?``excluded_models``[]`FQCNs of models to skip`type_overrides``[]`Manual TS type overrides per model per column`resource_namespace``App\Http\Resources`Namespace to scan for API resources`resource_directory``app/Http/Resources`Directory path corresponding to the resource namespace`resource_output_directory``resources/js/types/resources`Where resource `.d.ts` files are written`include_resources``false`Generate resource types by default (overridden by CLI flags)`excluded_resources``[]`FQCNs of resources to skip### Type Overrides Example

[](#type-overrides-example)

```
// config/typescript-generator.php
'type_overrides' => [
    App\Models\User::class => [
        'metadata' => '{ avatar: string; theme: "light" | "dark" }',
        'role'     => '"admin" | "editor" | "viewer"',
    ],
],
```

---

How Type Resolution Works
-------------------------

[](#how-type-resolution-works)

For each column the generator applies this priority:

1. **Manual override** (`type_overrides` in config) — highest priority
2. **Laravel cast** (`$casts` on the model) — takes precedence over raw DB type
3. **Database column type** (via schema introspection) — fallback

This means your casts are always respected. If you cast a `json` column to `array`, the TS type will be `unknown[]`, not `Record`.

---

Tip: Add to Your Workflow
-------------------------

[](#tip-add-to-your-workflow)

Add it to your `composer.json` scripts so types regenerate after migrations:

```
{
    "scripts": {
        "post-migrate": [
            "php artisan types:generate"
        ]
    }
}
```

Or add it to a git pre-commit hook or CI pipeline to keep types in sync.

---

License
-------

[](#license)

MIT

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance82

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

94d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d69f33cc173ab9f35c67cb1bbaabeb0b705f983f65253393525ccfc539abc260?d=identicon)[SynergiTech](/maintainers/SynergiTech)

---

Top Contributors

[![morganarnel](https://avatars.githubusercontent.com/u/84181964?v=4)](https://github.com/morganarnel "morganarnel (12 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/synergitech-laravel-typescript-generator/health.svg)

```
[![Health](https://phpackages.com/badges/synergitech-laravel-typescript-generator/health.svg)](https://phpackages.com/packages/synergitech-laravel-typescript-generator)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.3M347](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M246](/packages/laravel-ai)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k96.5k1](/packages/mike-bronner-laravel-model-caching)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
