Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Typescript Transformer Laravel Package

spatie/laravel-typescript-transformer

Generate TypeScript types from your Laravel/PHP code. Convert classes, DTOs and enums (with attributes) into accurate TS definitions, supporting nullable fields, complex/generic types, and even TypeScript function generation via a simple CLI/workflow.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Enhanced Type Safety for HTTP Methods: The release refines the method type from string to a literal union ('get' | 'post' | 'put' | 'patch' | 'delete'), directly addressing frontend framework compatibility (e.g., Inertia.js, Livewire). This aligns with type-driven development and reduces runtime assertions.
  • Configurable HTTP Method Filtering: The httpMethodsPriority option enables explicit control over emitted methods, improving frontend type clarity by excluding non-standard methods (e.g., HEAD, OPTIONS) by default. Ideal for SPA/MPA projects where these methods are rarely used.
  • Backward Compatibility: The change is non-breaking—existing projects continue to work, but generated types become more precise. Custom configurations (e.g., including HEAD) are supported via the new option.
  • Limited Scope Retained: The package remains focused on type transformation, not runtime logic. Complementary tools (e.g., spatie/laravel-api-documentation) are still required for full API alignment.

Integration Feasibility

  • Zero-Migration Overhead: The update is opt-in—existing projects require only configuration updates (e.g., httpMethodsPriority). No breaking changes to existing type outputs.
  • PHP/TypeScript Alignment: The literal union type ensures zero-cost abstractions for frontend frameworks expecting strict HTTP methods (e.g., Inertia’s visit()).
  • Configurability: Teams can customize emitted methods via httpMethodsPriority, e.g., for GraphQL-like endpoints or custom verbs (e.g., ['get', 'post', 'head']).
  • Build Process Impact: No changes to the transformation pipeline—outputs remain .d.ts files, but with refined type granularity.

Technical Risk

  • Frontend Framework Dependency: The improvement assumes frontend usage of libraries expecting strict HTTP methods (e.g., Inertia, Axios). Projects using dynamic API clients (e.g., OpenAPI-generated) may see minimal benefit.
  • Custom HTTP Methods: Projects using non-standard verbs (e.g., PURGE, SEARCH) must explicitly include them in httpMethodsPriority to avoid omission.
  • IDE/Tooling Sync: Generated types with literal unions may require TypeScript config updates (e.g., strict: true) to avoid implicit any fallbacks.
  • Performance: Negligible impact—type refinement happens at build time, with no runtime overhead.

Key Questions

  1. Frontend Ecosystem:
    • Does the project use Inertia.js, Livewire, or similar frameworks that benefit from strict HTTP method types?
    • Are there custom HTTP methods (e.g., PATCH, PROPFIND) that must be preserved in generated types?
  2. Configuration Strategy:
    • Should httpMethodsPriority be hardcoded in config/typescript-transformer.php or dynamic (e.g., per-route)?
    • How will teams override defaults (e.g., including HEAD for caching proxies)?
  3. TypeScript Strictness:
    • Is the frontend configured with strict: true in tsconfig.json? Literal unions may expose implicit any issues elsewhere.
  4. Deprecation Path:
    • If future releases deprecate the string fallback, how will legacy frontend code adapt?
  5. Custom Method Handling:
    • How will teams document the need to include non-standard methods in httpMethodsPriority?

Integration Approach

Stack Fit

  • Best For:
    • Laravel + Inertia.js/Livewire projects where strict HTTP method types improve developer experience.
    • API-first SPAs using Axios/Fetch with typed routes (e.g., router.visit('/users', { method: 'get' })).
    • Teams prioritizing type safety over minimalism in generated types.
  • Less Ideal For:
    • Projects using OpenAPI/Swagger for type generation (redundant benefit).
    • Legacy frontend codebases unable to adopt strict HTTP method types.
    • Non-TypeScript frontends (e.g., JavaScript, Python).

Migration Path

  1. Assess Impact:
    • Audit frontend route handlers to confirm compatibility with literal HTTP methods.
    • Check for custom HTTP verbs that may need inclusion in httpMethodsPriority.
  2. Configure Defaults:
    • Update config/typescript-transformer.php:
      'http_methods_priority' => ['get', 'post', 'put', 'patch', 'delete'],
      
    • For custom needs (e.g., HEAD):
      'http_methods_priority' => ['get', 'post', 'head'],
      
  3. Incremental Rollout:
    • Regenerate types with php artisan typescript-transformer:transform.
    • Validate .d.ts files for correct method unions (e.g., method: 'get' | 'post').
  4. Frontend Adoption:
    • Update Inertia/Livewire route calls to use typed methods:
      router.visit('/users', { method: 'get' }); // No cast needed
      
    • For Axios/Fetch, leverage the refined types in interceptors or clients.

Compatibility

  • Laravel: Works with Laravel 8+ (PHP 8.0+). No breaking changes to core functionality.
  • TypeScript: Outputs TypeScript 4.0+ literal unions. Ensure tsconfig.json has:
    {
      "strict": true,
      "noImplicitAny": true
    }
    
  • Frontend Frameworks:
    • Inertia.js: Directly benefits from method unions in visit() calls.
    • Livewire: Improves type safety for form submissions (e.g., method: 'post').
    • Axios: Types will align with AxiosRequestConfig['method'].
  • Custom Transformers: Existing hooks (e.g., TransformClass) remain unaffected.

Sequencing

  1. Update Package:
    composer require spatie/laravel-typescript-transformer --dev
    
  2. Configure:
    • Publish config: php artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider".
    • Set http_methods_priority in config/typescript-transformer.php.
  3. Test Locally:
    • Run php artisan typescript-transformer:transform.
    • Verify .d.ts files include literal method unions (e.g., method: 'get' | 'post').
  4. CI/CD Integration:
    • Add to composer.json scripts (unchanged):
      "post-update-cmd": ["@typescript-transformer:transform"]
      
    • Update frontend CI to validate generated types (e.g., tsc --noEmit).

Operational Impact

Maintenance

  • Pros:
    • Reduced Type Assertions: Frontend code no longer needs as const or type casts for HTTP methods.
    • Cleaner Generated Types: Exclusion of HEAD/OPTIONS reduces noise in .d.ts files.
    • Config-Driven: httpMethodsPriority centralizes control over emitted methods.
  • Cons:
    • Frontend Dependency: Teams using dynamic method strings (e.g., method: string) may need updates.
    • Custom Methods: Projects with non-standard verbs require explicit configuration.
    • TypeScript Strictness: May expose implicit any issues in unrelated parts of the codebase.
    • Documentation Gap: Teams must explicitly document how to include custom methods in httpMethodsPriority.

Support

  • Debugging:
    • Missing Methods: If HEAD is omitted unexpectedly, check httpMethodsPriority config.
    • Type Errors: Ensure tsconfig.json has strict: true to catch literal union mismatches.
    • Inertia/Livewire: Verify route handlers use the new typed methods (e.g., method: 'post').
  • Frontend Guidance:
    • Document the new type structure for frontend teams (e.g., RouteDefinition['method'] is now a union).
    • Provide examples for custom HTTP methods in httpMethodsPriority.
  • Community:
    • Active maintainer (Spatie) with clear documentation for the new feature. Community feedback suggests low risk for the change.

Scaling

  • Performance:
    • Negligible impact: Type refinement is a compile-time operation with no runtime cost.
    • Large Codebases: The httpMethodsPriority filter reduces output size, improving build times.
  • Configuration Management:
    • Centralized Control: `http
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai