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

Convert PHP classes, enums, and more into TypeScript types automatically in Laravel. Mark PHP code with attributes, handle complex types and generics, and generate TS-friendly definitions (and even functions) to keep frontend types in sync.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Full-Stack Type Safety: Enables seamless type synchronization between PHP backend (Laravel) and TypeScript frontend, reducing runtime errors and improving developer experience.
  • Developer Productivity: Automates boilerplate type generation for models, DTOs, controllers, and routes, accelerating frontend-backend alignment.
  • API Contracts: Generates precise TypeScript interfaces for API responses, requests, and route parameters, ensuring consistent client-side usage.
  • Migration Strategy: Supports incremental adoption (e.g., annotating critical models first) without requiring full frontend rewrite.
  • Tooling Integration: Complements existing Laravel tooling (e.g., Laravel Sanctum, API Resources) by providing typed abstractions for frontend consumption.
  • Roadmap Alignment: Critical for projects prioritizing:
    • Monorepo architectures (PHP + TypeScript in same repo).
    • Backend-for-Frontend (BFF) patterns.
    • Progressive web apps with tight Laravel integration.

Build vs. Buy:

  • Buy: Avoids reinventing type reflection/transpilation logic; leverages battle-tested Spatie infrastructure.
  • Build: Only if needing custom transformations beyond Spatie’s scope (e.g., domain-specific PHP constructs).

When to Consider This Package

Adopt when:

  • Your team uses Laravel 10+ and PHP 8.2+ (hard dependency).
  • Frontend relies on TypeScript and needs real-time API type safety.
  • You maintain complex PHP models/DTOs (e.g., Eloquent, Spatie DTOs) that require frontend types.
  • Controller/route generation is a pain point (e.g., manual OpenAPI docs or ad-hoc type definitions).
  • Watch mode is valuable for rapid development (auto-updates TypeScript on PHP changes).

Look elsewhere if:

  • Using older Laravel/PHP versions (pre-10/8.2).
  • Frontend is not TypeScript (e.g., JavaScript, React Native).
  • Types are trivial (e.g., simple CRUD apps with no custom DTOs).
  • Need custom PHP-to-TypeScript mappings beyond standard types (e.g., proprietary PHP extensions).
  • Prefer manual type definitions (e.g., for design flexibility).

How to Pitch It (Stakeholders)

For Executives: "This package bridges Laravel’s PHP backend with TypeScript frontends by auto-generating type definitions for models, controllers, and routes. It eliminates manual type syncing—saving dev time and reducing API errors. For example, a User model with 20 fields generates a TypeScript interface instantly, ensuring frontend forms and API calls stay in sync. Watch mode even updates types in real-time as the backend evolves. Ideal for scaling teams where backend changes frequently impact frontend consumers."

For Engineers: *"Spatie’s Laravel-TypeScript-Transformer lets you annotate PHP classes with [TypeScript] and generate matching TypeScript types via php artisan typescript:transform. Key features:

  • Controller/Route Types: Auto-generates typed route helpers (e.g., route<‘user.show’>(userId: number)).
  • Complex Support: Handles enums, generics, and Laravel collections (e.g., Collection<User>User[]).
  • Watch Mode: Runs php artisan typescript:watch to auto-regen types on file changes.
  • Customization: Extend with custom transformers for non-standard PHP types. Requires Laravel 10+ and PHP 8.2; replaces manual OpenAPI or ad-hoc type definitions."*

For Frontend Teams: *"No more guessing API shapes! This tool auto-generates TypeScript types for your Laravel backend, so:

  • Your User model’s fields become a User interface.
  • Controller actions yield typed request/response interfaces.
  • Route parameters are validated at compile time. Example: A StoreUserRequest DTO becomes a TypeScript type with all validation rules. Works with Laravel’s built-in validation and Sanctum auth."*
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport