You can install the package via composer:
composer require spatie/laravel-typescript-transformer
When using Laravel, first install the specific TypeScriptTransformerServiceProvider:
php artisan typescript:install
This command will create a TypeScriptTransformerServiceProvider in your app/Providers directory. Which looks like
this:
class TypeScriptTransformerServiceProvider extends BaseTypeScriptTransformerServiceProvider
{
protected function configure(TypeScriptTransformerConfigFactory $config): void
{
$config;
}
}
And it will also register the service provider in your bootstrap/providers.php file (when running Laravel 11 or
above). Or in your config/app.php file when running Laravel 10 or below.
Now you can transform types as such:
php artisan typescript:transform
Since we haven't configured TypeScript transformer yet, this command won't do anything.
In order to configure TypeScript Transformer, we recommend you to now continue reading the documentation on the
framework-agnostic getting started section. The docs will
explain how to configure the package which is by modifying the $config object we saw earlier in the
TypeScriptTransformerServiceProvider.
After you're done reading the framework-agnostic docs, you can return here to read about Laravel-specific features this
package provides.
##What the Laravel package provides
Out of the box, the Laravel package automatically configures a few things for you:
CarbonInterface (and Carbon) types are replaced with string in TypeScript
- The
AttributedClassTransformer is replaced with LaravelAttributedClassTransformer which adds proper handling for
Laravel's Collection and EloquentCollection as array-like structures
- TypeScript types are generated for Laravel's pagination classes:
LengthAwarePaginator and CursorPaginator. These
types include the full pagination structure with data, links andmeta properties, so you can use them in your
frontend code without defining them yourself.
These are all configured through the LaravelTypeScriptTransformerExtension which is loaded automatically when using
the Laravel Data
or controllers extensions. If you're not using either of those
but still want the base Laravel types, you can add the extension manually:
use Spatie\LaravelTypeScriptTransformer\LaravelTypeScriptTransformerExtension;
protected function configure(TypeScriptTransformerConfigFactory $config): void
{
$config->extension(new LaravelTypeScriptTransformerExtension());
}