This is the documentation for
v1 but the latest version is
v4
.
You can switch versions in the menu on the left/at the top.
Check your current version with the following command:
composer show spatie/laravel-data
Thanks to the typescript-transformer package, it is possible to automatically transform data objects into TypeScript definitions.
For example, the following data object:
class DataObject extends Data{
public function __construct(
public null|int $nullable,
public int $int,
public bool $bool,
public string $string,
public float $float,
/** @var string[] */
public array $array,
public Lazy|string $lazy,
public SimpleData $simpleData,
/** @var \Spatie\LaravelData\Tests\Fakes\SimpleData[] */
public DataCollection $dataCollection,
)
{
}
}
Would be transformed to the following TypeScript type:
{
nullable: number | null;
int: number;
bool: boolean;
string: string;
float: number;
array: Array<string>;
lazy?: string;
simpleData: SimpleData;
dataCollection: Array<SimpleData>;
}
To enable this, add the DataTypeScriptTransformer
transformer to the transformers in the typescript-transformer.php
config file.
You then should annotate each data object with a /** @typescript */
annotation or a #[TypeScript]
attribute.
If you want to transform all the data objects within your application to TypeScript, you can use the DataTypeScriptCollector
, which should be added to the collectors in typescript-transformer.php
.