Skip to main content
Thanks to the typescript-transformer package, you can automatically transform data objects into TypeScript definitions.

Example Transformation

This PHP data object:
Transforms to this TypeScript type:

Installation

Install the TypeScript transformer package:
Publish the config file:
Add the transformer to typescript-transformer.php:
If you’re using the DtoTransformer provided by the package, put the DataTypeScriptTransformer before it.

Usage

Annotate data objects you want to transform:
Or use the attribute:
Generate TypeScript definitions:

Transform All Data Objects

Use the DataTypeScriptCollector to automatically transform all data objects: In typescript-transformer.php:
If you’re using the DefaultCollector, put the DataTypeScriptCollector before it.

Optional Types

Lazy and optional properties become optional in TypeScript:
Transforms to:

Optional Without Lazy/Optional Types

Use the Optional attribute from typescript-transformer:
Alias the TypeScript Optional attribute to avoid conflicts with Laravel Data’s Optional type.