TypeScript Generation
Learn how to automatically generate TypeScript types from your DTOs.
Introduction
Section titled “Introduction”SimpleDTO can automatically generate TypeScript interfaces and types from your PHP DTOs:
- Automatic Generation - Generate TypeScript from PHP
- Type Mapping - PHP types → TypeScript types
- Nested DTOs - Automatic nested type generation
- Enums - PHP enums → TypeScript enums
- Arrays & Collections - Proper array typing
- Optional Properties - Nullable → optional
- JSDoc Comments - Include PHP docblocks
Quick Start
Section titled “Quick Start”Laravel
Section titled “Laravel”php artisan dto:typescriptSymfony
Section titled “Symfony”bin/console dto:typescriptPlain PHP
Section titled “Plain PHP”use event4u\DataHelpers\SimpleDTO\TypeScript\Generator;
$generator = new Generator();$generator->generate( dtoPath: __DIR__ . '/src/DTO', outputPath: __DIR__ . '/frontend/types');Basic Example
Section titled “Basic Example”PHP DTO
Section titled “PHP DTO”class UserDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $name, public readonly string $email, public readonly ?string $phone = null, ) {}}Generated TypeScript
Section titled “Generated TypeScript”export interface UserDTO { id: number; name: string; email: string; phone?: string | null;}Type Mapping
Section titled “Type Mapping”Primitive Types
Section titled “Primitive Types”// PHPpublic readonly string $name;public readonly int $age;public readonly float $price;public readonly bool $active;
// TypeScriptname: string;age: number;price: number;active: boolean;Arrays
Section titled “Arrays”// PHPpublic readonly array $tags;/** @var string[] */public readonly array $categories;
// TypeScripttags: any[];categories: string[];Nullable Types
Section titled “Nullable Types”// PHPpublic readonly ?string $middleName = null;public readonly ?int $age = null;
// TypeScriptmiddleName?: string | null;age?: number | null;Union Types
Section titled “Union Types”// PHPpublic readonly string|int $id;
// TypeScriptid: string | number;Nested DTOs
Section titled “Nested DTOs”PHP DTOs
Section titled “PHP DTOs”class AddressDTO extends SimpleDTO{ public function __construct( public readonly string $street, public readonly string $city, public readonly string $country, ) {}}
class UserDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $name, public readonly AddressDTO $address, ) {}}Generated TypeScript
Section titled “Generated TypeScript”export interface AddressDTO { street: string; city: string; country: string;}
export interface UserDTO { id: number; name: string; address: AddressDTO;}Collections
Section titled “Collections”PHP DTO
Section titled “PHP DTO”class PostDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $title, /** @var CommentDTO[] */ public readonly array $comments, ) {}}
class CommentDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $content, ) {}}Generated TypeScript
Section titled “Generated TypeScript”export interface CommentDTO { id: number; content: string;}
export interface PostDTO { id: number; title: string; comments: CommentDTO[];}PHP Enum
Section titled “PHP Enum”enum UserRole: string{ case ADMIN = 'admin'; case USER = 'user'; case GUEST = 'guest';}
class UserDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $name, public readonly UserRole $role, ) {}}Generated TypeScript
Section titled “Generated TypeScript”export enum UserRole { ADMIN = 'admin', USER = 'user', GUEST = 'guest',}
export interface UserDTO { id: number; name: string; role: UserRole;}Configuration
Section titled “Configuration”Laravel
Section titled “Laravel”return [ 'typescript' => [ 'output' => resource_path('js/types/dtos.ts'), 'export_type' => 'export', // 'export', 'declare', or '' 'include_comments' => true, 'sort_properties' => false, ],];Symfony
Section titled “Symfony”simple_dto: typescript: output: '%kernel.project_dir%/assets/types/dtos.ts' export_type: 'export' include_comments: true sort_properties: falseWatch Mode
Section titled “Watch Mode”Laravel
Section titled “Laravel”php artisan dto:typescript --watchSymfony
Section titled “Symfony”bin/console dto:typescript --watchAutomatically regenerates TypeScript when DTOs change.
CI/CD Integration
Section titled “CI/CD Integration”Check if Types are Up-to-Date
Section titled “Check if Types are Up-to-Date”# Laravelphp artisan dto:typescript --check
# Symfonybin/console dto:typescript --checkReturns exit code 1 if types are outdated.
GitHub Actions Example
Section titled “GitHub Actions Example”name: CI
on: [push, pull_request]
jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Install dependencies run: composer install - name: Check TypeScript types run: php artisan dto:typescript --checkBest Practices
Section titled “Best Practices”1. Always Use Type Hints
Section titled “1. Always Use Type Hints”// ✅ Goodpublic readonly string $name;
// ❌ Bad - generates 'any'public readonly $name;2. Document Array Types
Section titled “2. Document Array Types”// ✅ Good/** @var string[] */public readonly array $tags;
// ❌ Bad - generates 'any[]'public readonly array $tags;3. Use Enums for Constants
Section titled “3. Use Enums for Constants”// ✅ Goodpublic readonly UserRole $role;
// ❌ Badpublic readonly string $role;4. Generate on Deployment
Section titled “4. Generate on Deployment”# Add to deployment scriptphp artisan dto:typescriptCode Examples
Section titled “Code Examples”The following working examples demonstrate this feature:
- Basic Generation - Generate TypeScript types
- Generator Options - Customizing generation
All examples are fully tested and can be run directly.
Related Tests
Section titled “Related Tests”The functionality is thoroughly tested. Key test files:
- TypeScriptGeneratorTest.php - TypeScript generation tests
Run the tests:
# Run teststask test:unit -- --filter=TypeScriptSee Also
Section titled “See Also”- Artisan Commands - Laravel commands
- Console Commands - Symfony commands
- Creating DTOs - DTO creation guide