diephp/laravel-resources-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:
return [] arrays#[ArrayShape(...)]$fillabletoArray() methods that build the response through a variable like $data['field'] = ...; return $data;enumIf a type cannot be determined safely, it falls back to any.
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 project and then opened for everyone who prefers to keep server resources fully under control.
enum inside resources.type aliases for PHP enums.toArray() methods that build payload through a variable like $data = []; $data['x'] = ...; return $data;.interface and type.^8.1^9.0 and compatible with newer versions ^13.0 that keep standard resource behaviorinterface and typeVersion 2.0 is mostly source-compatible in usage, but there are a few important changes.
8.1 instead of 8.0.interface. Enum-based definitions can now be emitted as TypeScript type.1.x.
For example:
anyJsonResource with return [].#[ArrayShape(...)].@return array{...}.any when the package cannot safely infer a type.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
Run the generator:
php artisan diephp:generate-typescript-interfaces
Default config:
'resources_dir' => 'app/Http/Resources',
'output_typescript_file' => 'resources/ts/Resources.ts',
The generator can infer structure from:
return []#[ArrayShape(...)]@return array{...}toArray()$fillableclass ExampleResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => (int) $this->id,
'name' => (string) $this->name,
];
}
}
export interface ExampleResource {
id: number;
name: string;
}
ArrayShapeuse 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;
}
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;
}
class UserDto
{
public string $name;
public string $email;
public int $age;
}
export interface UserDto {
name: string;
email: string;
age: number;
}
$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;
}
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;
}
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;
}
class EnumValueResource extends JsonResource
{
public function toArray($request)
{
return StatusEnum::Published;
}
}
export type EnumValueResource = StatusEnum;
ArrayShape -> PHPDoc -> AST analysis of toArray() -> runtime fallback.type aliases.any.If you are publishing or searching for the package, the main focus of 2.0 is:
composer test
MIT
How can I help you explore Laravel packages today?