Using transformers | typescript-transformer | Spatie

 SPATIE

  TypeScript Transformer
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Typescript-transformer](https://spatie.be/docs/typescript-transformer/v2)  Usage  Using transformers

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/typescript-transformer/v3) [v2](https://spatie.be/docs/typescript-transformer/v2) [v1](https://spatie.be/docs/typescript-transformer/v1)

- [ Introduction ](https://spatie.be/docs/typescript-transformer/v2/introduction)
- [ Postcardware ](https://spatie.be/docs/typescript-transformer/v2/postcardware)
- [ Installation ](https://spatie.be/docs/typescript-transformer/v2/installation)
- [ Under the hood ](https://spatie.be/docs/typescript-transformer/v2/under-the-hood)
- [ Questions &amp; issues ](https://spatie.be/docs/typescript-transformer/v2/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/typescript-transformer/v2/changelog)
- [ About us ](https://spatie.be/docs/typescript-transformer/v2/about-us)

Usage
-----

- [ General overview ](https://spatie.be/docs/typescript-transformer/v2/usage/general-overview)
- [ Getting started ](https://spatie.be/docs/typescript-transformer/v2/usage/getting-started)
- [ Describing types ](https://spatie.be/docs/typescript-transformer/v2/usage/annotations)
- [ Using transformers ](https://spatie.be/docs/typescript-transformer/v2/usage/using-transformers)
- [ Collectors ](https://spatie.be/docs/typescript-transformer/v2/usage/selecting-classes-using-collectors)
- [ Writers ](https://spatie.be/docs/typescript-transformer/v2/usage/writers)
- [ Formatters ](https://spatie.be/docs/typescript-transformer/v2/usage/formatters)

Writing transformers
--------------------

- [ Getting started ](https://spatie.be/docs/typescript-transformer/v2/transformers/getting-started)
- [ Type reflectors ](https://spatie.be/docs/typescript-transformer/v2/transformers/type-reflectors)
- [ Type processors ](https://spatie.be/docs/typescript-transformer/v2/transformers/type-processors)

Transforming PHP classes
------------------------

- [ Typing properties ](https://spatie.be/docs/typescript-transformer/v2/dtos/typing-properties)
- [ Customization ](https://spatie.be/docs/typescript-transformer/v2/dtos/transforming-dtos)

Laravel
-------

- [ Installation and setup ](https://spatie.be/docs/typescript-transformer/v2/laravel/installation-and-setup)
- [ Executing the transform command ](https://spatie.be/docs/typescript-transformer/v2/laravel/executing-the-transform-command)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/typescript-transformer                                                                                                                                                                                                                                    `

Using transformers
==================

###  On this page

1. [ Default transformers ](#content-default-transformers)
2. [ Writing your own transformers ](#content-writing-your-own-transformers)

Transformers are the heart of the package. They take a PHP class and try to make a TypeScript definition out of it.

Default transformers
--------------------------------------------------------------------------------------------------------------------

The package comes with a few transformers out of the box:

- `EnumTransformer`: this transforms a PHP 8.1 native enum
- `MyclabsEnumTransformer`: this transforms an enum from the [`myclabs\php-enum`](https://github.com/myclabs/php-enum) package
- `SpatieEnumTransformer`: this transforms an enum from the [`spatie\enum`](https://github.com/spatie/enum) package
- `DtoTransformer`: a powerful transformer that transforms entire classes and their properties, you can read more about it [here](/docs/typescript-transformer/v2/dtos/typing-properties)
- `InterfaceTransformer`: this transforms a PHP interface and its functions to a Typescript interface. If used, this transformer should always be included before the `DtoTransformer`.

[The laravel package](/docs/typescript-transformer/v2/laravel/installation-and-setup) has some extra transformers:

- `SpatieStateTransformer`: this transforms a state from the [`spatie\laravel-model-states`](https://github.com/spatie/laravel-model-status) package
- `DtoTransformer`: a more Laravel specific transformer based upon the default `DtoTransformer`

There are also some packages with community transformers:

- A [transformer](https://github.com/wt-health/laravel-enum-transformer) for `bensampo/laravel-enum` enums

If you've written a transformer package, let us know, and we add it to the list!

You should supply a list of transformers the package should use in your config. The order of transformers matters and can lead to unexpected results if in the wrong order. A PHP declaration (e.g. classes, enums) will go through each transformer and stop once a transformer is able to handle it; this is a problem if `DtoTransformer` is listed before an enum transformer since `DtoTransformer` will incorrectly handle an enum as a class and never allow `MyclabsEnumTransformer` to handle it.

```
$config = TypeScriptTransformerConfig::create()
    ->transformers([MyclabsEnumTransformer::class, DtoTransformer::class])
   ...
```

### Transforming enums

The package ships with three enum transformers out of the box, by default these enums are transformed to TypeScript types like this:

```
type Language = 'JS' | 'PHP';
```

It is possible to transform them to native TypeScript enums by changing the config:

```
$config = TypeScriptTransformerConfig::create()
    ->transformToNativeEnums()
   ...
```

A transformed enum now looks like this:

```
enum Language {'JS' = 'JS', 'PHP' = 'PHP'};
```

Writing your own transformers
-----------------------------------------------------------------------------------------------------------------------------------------------

We've added a whole section in the docs about [writing transformers](../transformers/getting-started).
