Type reflectors | typescript-transformer | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Typescript-transformer](https://spatie.be/docs/typescript-transformer/v2)  Transformers  Type reflectors

 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                                                                                                                                                                                                                                    `

Type reflectors
===============

Writing transformers can be complicated since there's a lot to keep in mind when trying to resolve the types within PHP classes.

TypeReflectors can help you with this. They will take a `ReflectionMethod`, `ReflectionProperty`or `ReflectionParameter` and convert it into a `Type` which can be easily transpiled to TypeScript.

A type reflector uses the following information to deduce a type:

- attributes added to the PHP definition
- an annotation was added with the PHP definition
- the type is written in PHP, and if it is nullable

It will use all this information and creates a `Type` object from the [phpDocumentor/TypeResolver](https://github.com/phpDocumentor/TypeResolver) package, examples of such types are:

- Array\_
- Boolean
- Compound
- Object\_
- Void
- [and many more](https://github.com/phpDocumentor/TypeResolver/tree/1.x/src/Types)

These types can be easily transpiled to TypeScript. Let's take a look at an example:

```
class Properties{
    #[LiteralTypeScriptType('unknown')]
    public $propertyWithAttribute;

    /** @var int */
    public $propertyWithAnnotation;

    public bool $propertyWithType;

    public ?string $propertyWithNullableType;
};
```

We can now write a transformer that uses the `TransformsTypes` trait. This trait adds the `reflectionToTypeScript` method to your transformer, which takes a reflected entity and a missing symbols collection and transforms it to Typescript.

```

class PropertyTransformer implements Transformer{
    use TransformsTypes;

    public function transform(ReflectionClass $class, string $name) : ?TransformedType
    {
        $missingSymbols = new MissingSymbolsCollection();

        $properties = array_map(
            fn(ReflectionProperty $reflection) => "{$reflection->name}: {$this->reflectionToTypeScript($reflection, $missingSymbols)};",
            $class->getProperties()
        );

        return TransformedType::create(
            $class,
            $name,
            '{'. join($properties) . '}',
            $missingSymbols
        );
    }
}
```

This transformer will transform the `Properties` class into:

```
{
    propertyWithAttribute: unknown;
    propertyWithAnnotation: number;
    propertyWithType: boolean;
    propertyWithNullableType: ?string;
}
```
