PHP code example of brunoscode / laravel-ts-annotations
1. Go to this page and download the library: Download brunoscode/laravel-ts-annotations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brunoscode / laravel-ts-annotations example snippets
// Raw TypeScript — full control
#[TS(<<<'TS'
export type UserResponse = {
id: number;
name: string;
role: 'admin' | 'editor' | 'viewer';
}
TS)]
class UserResource extends JsonResource {}
// Auto-inferred from class properties
#[TSType]
class UserData
{
public function __construct(
public readonly int $id,
public readonly string $name,
public readonly ?string $email,
) {}
}
// Auto-inferred from PHP enum
#[TSEnum]
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
}
use BrunosCode\LaravelTsAnnotations\Attributes\TS;
#[TS(<<<'TS'
export type UserResponse = {
id: number;
name: string;
role: 'admin' | 'editor' | 'viewer';
}
TS)]
class UserResource extends JsonResource {}
class UserController extends Controller
{
#[TS(<<<'TS'
export type UserListResponse = {
data: UserResponse[];
total: number;
per_page: number;
}
TS)]
public function index(): JsonResponse { ... }
#[TS(<<<'TS'
export type UserStoreResponse = {
data: UserResponse;
message: string;
}
TS)]
public function store(StoreUserRequest $request): JsonResponse { ... }
}
use BrunosCode\LaravelTsAnnotations\Attributes\TSType;
#[TSType]
class OrderData
{
public function __construct(
public readonly int $id,
public readonly string $reference,
public readonly float $total,
public readonly bool $paid,
public readonly ?string $note,
) {}
}
#[TSType(name: 'IOrder')]
class OrderData { ... }
// → export type IOrder = { ... }
use BrunosCode\LaravelTsAnnotations\Attributes\TSEnum;
// String-backed
#[TSEnum]
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
case Pending = 'pending';
}
// → export enum Status { Active = 'active', Inactive = 'inactive', Pending = 'pending', }
// Int-backed
#[TSEnum]
enum Priority: int
{
case Low = 1;
case Medium = 2;
case High = 3;
}
// → export enum Priority { Low = 1, Medium = 2, High = 3, }
// Unit enum (no backing type) — case name used as string value
#[TSEnum]
enum Direction
{
case North;
case South;
case East;
case West;
}
// → export enum Direction { North = 'North', South = 'South', East = 'East', West = 'West', }