diephp/laravel-resources-typescript
Version 2.0.2 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 9, 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.
Maintenance release, no API changes and no change to supported Laravel or PHP versions:
laravel/framework widened from ^9.0 to ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0,
so the test suite can run on any supported framework version instead of an unreleased 9.x branch.minimum-stability switched from dev to stable, so a plain composer update no longer pulls
development branches.jetbrains/phpstorm-attributes pinned to a stable ^1.0 instead of dev-master.phpunit/phpunit widened to ^10.5 || ^11.5 || ^12.0.8.2–8.4) and a Dependabot configuration for dev dependencies.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.19, 10, 11, 12, 13 — any version that keeps standard resource behaviourinterface and typeLaravel is not a dependency of this package. The framework is only needed for the artisan command
and the service provider, so it is listed in require-dev (for this repository's own test suite) and
in suggest. Neither of those affects your project: Composer ignores a library's require-dev, so the
package installs next to any Laravel version and never forces an upgrade.
The only production dependency is nikic/php-parser.
The package has no runtime dependency on laravel/framework, so no framework advisory can reach a
consumer project through it. Advisories that automated scanners report against this repository refer to
the framework version used by the package's own test suite, not to anything shipped to users.
To keep that noise down without narrowing Laravel support:
require-dev spans ^9.0 through ^13.0, so a patched version is always inside the declared range
and the repository never pins an end-of-life branch.minimum-stability is stable, so composer update resolves to real releases instead of a
dev-master branch that the advisory database cannot match.composer.lock is not committed — this is a library, not an application — so no stale locked version
can be flagged.composer audit --no-dev, which audits exactly what consumers actually receive.Version 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
Check dependencies against the security advisory database:
composer audit
MIT
How can I help you explore Laravel packages today?