spatie/typescript-transformer
Convert PHP classes, enums, and DTOs into TypeScript types with spatie/typescript-transformer. It supports complex types, generics, and generated TypeScript functions, making frontend/backend type sharing simple.
.d.ts files that drift. This package converts annotated PHP classes, enums, interfaces, and DTOs into TypeScript types, so "buy" (adopt, MIT, zero license cost) is a low-commitment default unless we have a non-PHP backend.any at API boundaries) is usually gated on having real types for server payloads. This package is a prerequisite enabler for that work, and a cheap one to spike.@template), union/nullable types, and array shapes map to concrete product surfaces — status enums, paginated responses, form payloads. We can decide "ship the enum to the frontend as a union type" without a parallel spec document.ModuleWriter emits modules mirroring PHP namespaces for first-party frontend consumption; GlobalNamespaceWriter emits a single .d.ts for global-scope consumers. That affects import ergonomics and whether we can hand types to an external team.TransformedProvider mean we can inject types that don't originate from PHP classes (e.g., framework collection generics) and post-process the tree. Watch mode (beta) targets the edit loop. Both are candidates for a "reduce frontend friction" initiative rather than standalone roadmap items.Adopt when most of these hold:
npx prettier / eslint formatters) to the pipeline.Look elsewhere when:
.d.ts. Use this internally at most.DtoTransformer removed, TypeReflectors removed, inline types removed) and no upgrade guide. Budget a migration spike or stay pinned.For executives (one paragraph): A free, MIT-licensed, actively released Spatie package that generates TypeScript types directly from our PHP classes, enums, and DTOs. It removes a recurring category of frontend/backend integration bugs and unblocks tightening TypeScript across the app, at the cost of a Composer dependency and a CI step — no vendor contract, no per-seat licensing, reversible in a sprint if it doesn't pay off. Principal risk is maturity signals: modest adoption (~400 stars, no downstream dependents in our data) and a recent v3 rewrite with no upgrade path, so we pin the version and treat generated files as build artifacts.
For engineering (the substance): Attribute-driven transformation (#[TypeScript]) of classes, enums, interfaces, and DTOs. v3 replaces string generation with a real TypeScript AST plus a visitor pattern for replace/remove passes, swaps PHPDocumentor for PHPStan's type parser (better generics, array shapes, key-of/value-of, unions/intersections), and adds TransformedProvider for injecting types not backed by a PHP class — useful for framework generics. Two writers: per-namespace modules or a single global .d.ts. Prettier and ESLint formatters keep output reviewable, but they mean Node must exist in the build image. Watch mode is beta — do not put it in the critical path. Generation must be deterministic and checked in CI; the 3.1.1 fix that errors on a missing output directory rather than silently resolving to an empty path is exactly the failure mode to guard against.
Risks to state up front: v3 breaking changes with no migration guide (assume a spike, not a lift-and-shift); beta watch mode; a Node dependency for formatting; and a small ecosystem footprint, which argues for a thin internal wrapper so the package is swappable if we later move to schema-first.
How can I help you explore Laravel packages today?