โœ…
Laravel Validated DTO
View on GitHub
  • โœ…Validated DTO for Laravel
  • ๐Ÿš€Getting Started
    • Installation
    • Configuration
    • Upgrade Guide
    • Changelog
  • ๐Ÿ˜ŽThe Basics
    • Generating DTOs
    • Defining DTO Properties
    • Defining Validation Rules
    • Creating DTO Instances
    • Accessing DTO Data
    • Defining Default Values
    • Transforming DTO Data
    • Mapping DTO properties
    • Simple DTOs
    • Resource DTOs
    • Wireable DTOs
    • Lazy Validation
    • Generating TypeScript Definitions
  • ๐ŸŽจCustomize
    • Custom Error Messages and Attributes
    • Custom Exceptions
  • ๐ŸงชType Casting
    • Introduction
    • Available Types
    • Create Your Own Type Cast
    • Casting Eloquent Model properties to DTOs
Powered by GitBook
On this page
  • Step 1:
  • Step 2:
  • Step 3:
  • Step 4:

Was this helpful?

  1. The Basics

Generating TypeScript Definitions

Step 1:

Install the spatie/typescript-transformer package.

composer require spatie/typescript-transformer

Step 2:

Publish the typescript-transformer config.

php artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider"

Step 3:

Register the packages TypeScriptCollector and TypeScriptTransformer in the typescript-transformer config.

//config/typescript-transformer.php
   ...
    /*
     * Collectors will search for classes in the `auto_discover_types` paths and choose the correct
     * transformer to transform them. By default, we include a DefaultCollector which will search
     * for @typescript annotated and #[TypeScript] attributed classes to transform.
     */

    'collectors' => [
        Spatie\TypeScriptTransformer\Collectors\DefaultCollector::class,
        Spatie\TypeScriptTransformer\Collectors\EnumCollector::class,
+       WendellAdriel\ValidatedDTO\Support\TypeScriptCollector,
    ],

    /*
     * Transformers take PHP classes(e.g., enums) as an input and will output
     * a TypeScript representation of the PHP class.
     */

    'transformers' => [
        Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
        Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer::class,
+       WendellAdriel\ValidatedDTO\Support\TypeScriptTransformer,
    ],
   ...

Step 4:

Run the command to generate the TypeScript types.

 php artisan typescript:transform

By default, the definitions are added here: resources/types/generated.d.ts.

PreviousLazy ValidationNextCustom Error Messages and Attributes

Last updated 2 months ago

Was this helpful?

๐Ÿ˜Ž