PHPackages                             diephp/laravel-resources-typescript - 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. [API Development](/categories/api)
4. /
5. diephp/laravel-resources-typescript

ActiveLibrary[API Development](/categories/api)

diephp/laravel-resources-typescript
===================================

Generate TypeScript interfaces and type aliases from Laravel JsonResource classes, including enum support

v2.0.1(2mo ago)3163MITPHPPHP ^8.1

Since Feb 25Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/diephp/laravel-resources-typescript)[ Packagist](https://packagist.org/packages/diephp/laravel-resources-typescript)[ RSS](/packages/diephp-laravel-resources-typescript/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (7)Versions (5)Used By (0)

[![Total Downloads](https://camo.githubusercontent.com/2b4d0d48af7c47e40f17646afea70dc9badadbc3b89a61829232d950990c5d97/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6469657068702f6c61726176656c2d7265736f75726365732d74797065736372697074)](https://packagist.org/packages/diephp/laravel-resources-typescript)[![Latest Stable Version](https://camo.githubusercontent.com/a9f7f12f72916f4d5716ca6476fdb68184f9f20a895b58bac3915618885fa2d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6469657068702f6c61726176656c2d7265736f75726365732d74797065736372697074)](https://packagist.org/packages/diephp/laravel-resources-typescript)[![License](https://camo.githubusercontent.com/6bc139c0a8e37c534e2af349aecfdec4242d5e1fe9cbd862c5d125372023f909/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6469657068702f6c61726176656c2d7265736f75726365732d74797065736372697074)](https://packagist.org/packages/diephp/laravel-resources-typescript)

Laravel Resources to TypeScript
===============================

[](#laravel-resources-to-typescript)

Version `2.0` of the package.

Generate TypeScript `interface` and `type` definitions from standard Laravel resources without rewriting your API layer into DTO-only abstractions.

The package analyzes native Laravel resources and can extract structure from:

- direct `return []` arrays
- PHPDoc array shapes
- `#[ArrayShape(...)]`
- public typed properties on DTO-style classes
- Eloquent `$fillable`
- `toArray()` methods that build the response through a variable like `$data['field'] = ...; return $data;`
- resources and fields that return PHP `enum`

If a type cannot be determined safely, it falls back to `any`.

Project Story
-------------

[](#project-story)

This is a lightweight package for development-time generation of TypeScript interfaces from standard Laravel resources for Laravel `10`, `11`, `12`, `13` and PHP `8.1+`.

It was originally developed to satisfy a requirement to minimize the use of heavy packages in the [Restsify](https://restsify.com/en) project and then opened for everyone who prefers to keep server resources fully under control.

What's New in 2.0
-----------------

[](#whats-new-in-20)

- Support for PHP `enum` inside resources.
- Generation of TypeScript `type` aliases for PHP enums.
- Support for resources that return enum as the root value, not only arrays.
- Better AST analysis for `toArray()` methods that build payload through a variable like `$data = []; $data['x'] = ...; return $data;`.
- Support for optional fields created in conditional branches.
- Support for DTO-like classes with public typed properties.
- Recursive scanning of PHP files inside the configured resources directory.
- Cleaner TypeScript output generation with both `interface` and `type`.

Compatibility
-------------

[](#compatibility)

- PHP: `^8.1`
- Laravel: tested with `^9.0` and compatible with newer versions `^13.0` that keep standard resource behavior
- TypeScript output: supports both generated `interface` and `type`

Upgrading from 1.x
------------------

[](#upgrading-from-1x)

Version `2.0` is mostly source-compatible in usage, but there are a few important changes.

### Breaking changes

[](#breaking-changes)

- Minimum PHP version is now `8.1` instead of `8.0`.
- Generated output is no longer limited to `interface`. Enum-based definitions can now be emitted as TypeScript `type`.
- In some resources the generated types may become more precise than in `1.x`. For example:
    - enum fields now reference generated enum types
    - conditionally assigned fields can become optional
    - DTO public typed properties are now inferred instead of falling back to `any`

### What remains compatible

[](#what-remains-compatible)

- Standard Laravel `JsonResource` with `return []`.
- `#[ArrayShape(...)]`.
- PHPDoc `@return array{...}`.
- Nested resources and resource collections.
- Fallback to `any` when the package cannot safely infer a type.

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

[](#installation)

```
composer require --dev diephp/laravel-resources-typescript
```

For `2.0`, your project must run on PHP `8.1+`.

Publish the config if you want to customize paths:

```
php artisan vendor:publish --tag=resources2typescript
```

Usage
-----

[](#usage)

Run the generator:

```
php artisan diephp:generate-typescript-interfaces
```

Default config:

```
'resources_dir' => 'app/Http/Resources',
'output_typescript_file' => 'resources/ts/Resources.ts',
```

Supported Type Sources
----------------------

[](#supported-type-sources)

The generator can infer structure from:

- direct `return []`
- `#[ArrayShape(...)]`
- PHPDoc `@return array{...}`
- assignments through a temporary variable inside `toArray()`
- public typed properties on DTO classes
- model PHPDoc properties
- model `$fillable`
- enum values returned by fields or by the resource itself

Examples
--------

[](#examples)

### 1. Plain resource with casts

[](#1-plain-resource-with-casts)

```
class ExampleResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => (int) $this->id,
            'name' => (string) $this->name,
        ];
    }
}
```

```
export interface ExampleResource {
  id: number;
  name: string;
}
```

### 2. Resource with `ArrayShape`

[](#2-resource-with-arrayshape)

```
use JetBrains\PhpStorm\ArrayShape;

class ExampleResource extends JsonResource
{
    #[ArrayShape(['id' => 'int', 'name' => 'string'])]
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}
```

```
export interface ExampleResource {
  id: number;
  name: string;
}
```

### 3. Resource with PHPDoc array shape

[](#3-resource-with-phpdoc-array-shape)

```
class ExampleResource extends JsonResource
{
    /**
     * @return array{
     *     id: int,
     *     name: string,
     *     category: \App\Http\Resources\CategoryResource,
     * }
     */
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'category' => new CategoryResource($this->category),
        ];
    }
}
```

```
export interface ExampleResource {
  id: number;
  name: string;
  category: CategoryResource;
}
```

### 4. DTO or model-like class with typed public properties

[](#4-dto-or-model-like-class-with-typed-public-properties)

```
class UserDto
{
    public string $name;
    public string $email;
    public int $age;
}
```

```
export interface UserDto {
  name: string;
  email: string;
  age: number;
}
```

### 5. Model with PHPDoc properties or `$fillable`

[](#5-model-with-phpdoc-properties-or-fillable)

```
/**
 * @property string $domain
 * @property mixed $protocol
 */
class SiteModel extends Model
{
}
```

```
class ContactModel extends Model
{
    protected $fillable = ['name', 'email', 'phone'];
}
```

```
export interface SiteModel {
  domain: string;
  protocol: any;
}

export interface ContactModel {
  name: any;
  email: any;
  phone: any;
}
```

### 6. Resource built through a variable

[](#6-resource-built-through-a-variable)

```
class TimeResource extends JsonResource
{
    public function toArray($request): array
    {
        $result = [];
        $result['time'] = $this->resource->time;

        if ($this->resource->time_zone) {
            $result['time_zone'] = $this->resource->time_zone;
        }

        return $result;
    }
}
```

```
export interface TimeResource {
  time: any;
  time_zone?: any;
}
```

### 7. Enum field inside a resource

[](#7-enum-field-inside-a-resource)

```
enum StatusEnum: string
{
    case Draft = 'draft';
    case Published = 'published';
}

class StatusResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'status' => StatusEnum::Published,
        ];
    }
}
```

```
export type StatusEnum = 'draft' | 'published';

export interface StatusResource {
  status: StatusEnum;
}
```

### 8. Resource that returns enum as root value

[](#8-resource-that-returns-enum-as-root-value)

```
class EnumValueResource extends JsonResource
{
    public function toArray($request)
    {
        return StatusEnum::Published;
    }
}
```

```
export type EnumValueResource = StatusEnum;
```

Notes
-----

[](#notes)

- Priority is roughly: `ArrayShape` -&gt; PHPDoc -&gt; AST analysis of `toArray()` -&gt; runtime fallback.
- Enum definitions are generated as TypeScript `type` aliases.
- Nested resources are preserved as references to their generated TypeScript definitions.
- The scanner walks resource directories recursively.
- If the package cannot infer an exact type, it falls back to `any`.

Search Keywords
---------------

[](#search-keywords)

If you are publishing or searching for the package, the main focus of `2.0` is:

- Laravel Resource to TypeScript
- Laravel JsonResource TypeScript generator
- PHP enum to TypeScript
- API resource typings for Laravel

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

MIT

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance88

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Every ~144 days

Total

4

Last Release

61d ago

Major Versions

v0.1.5 → v2.0.02026-04-12

PHP version history (2 changes)v0.1.4PHP ^8.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/129999305?v=4)[Die PHP](/maintainers/diephp)[@diephp](https://github.com/diephp)

---

Top Contributors

[![AnatolyAstapov](https://avatars.githubusercontent.com/u/12308843?v=4)](https://github.com/AnatolyAstapov "AnatolyAstapov (15 commits)")

---

Tags

apiinterfaceslaravelenumlaravel-packagetypescriptresourcesphp-enumtypesgeneratecodegenlaravel typescripttypescript-generatorlaravel-resourcejsonresource

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/diephp-laravel-resources-typescript/health.svg)

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

###  Alternatives

[knuckleswtf/scribe

Generate API documentation for humans from your Laravel codebase.✍

2.3k14.2M62](/packages/knuckleswtf-scribe)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k11.2M100](/packages/dedoc-scramble)[joisarjignesh/bigbluebutton

BigBlueButton Server API Library for Laravel

162155.1k1](/packages/joisarjignesh-bigbluebutton)[wayofdev/laravel-symfony-serializer

📦 Laravel wrapper around Symfony Serializer.

2117.7k](/packages/wayofdev-laravel-symfony-serializer)[mahdimajidzadeh/laravel-unsplash

Laravel package for Unsplash Api

1111.4k](/packages/mahdimajidzadeh-laravel-unsplash)

PHPackages © 2026

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