Writers | 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  Writers

 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                                                                                                                                                                                                                                    `

Writers
=======

###  On this page

1. [ TypeDefinitionWriter ](#content-typedefinitionwriter)
2. [ ModuleWriter ](#content-modulewriter)
3. [ Building your own writer ](#content-building-your-own-writer)

When all types are transformed, the package will write them out in one big output file. A writer will determine how this output will look.

You can configure a writer as such:

```
$config = TypeScriptTransformerConfig::create()
    ->writer(TypeDefinitionWriter::class)
    ...
```

By default, the `TypeDefinitionWriter` is used when no writer was configured. You can use one of the Writers shipped with the package out of the box or write your own one.

TypeDefinitionWriter
--------------------------------------------------------------------------------------------------------------------

The `TypeDefinitionWriter` will group types into namespaces that follow the structure of the PHP namespaces.

Let's take a look at an example with two PHP classes:

```
namespace App\Enums;

#[TypeScript]
class Language extends Enum
{
    public const nl = 'nl';
    public const en = 'en';
    public const fr = 'fr';
}
```

and

```
namespace App\Models;

#[TypeScript]
class User
{
    public string $name;
    public \App\Enums\Language $language;
}
```

The written TypeScript will look like this:

```
namespace App.Enums {
    export type Language = 'nl' | 'en' | 'fr';
};

namespace App.Models {
    export type User = {
        name: string;
        language: App.Enums.Language
    };
};
```

ModuleWriter
--------------------------------------------------------------------------------------------

The `ModuleWriter` will ignore namespaces and list all the types as individual modules.

When we use the same classes from the previous example, the written TypeScript now looks like this:

```
export type Language = 'nl' | 'en' | 'fr';
export type User = {
    name: string;
    language: Language
};
```

Building your own writer
--------------------------------------------------------------------------------------------------------------------------------

A writer is a class implementing the `Writer` interface:

```
interface Writer
{
    public function format(TypesCollection $collection): string;

    public function replacesSymbolsWithFullyQualifiedIdentifiers(): bool;
}
```

The `format` method takes a `TypesCollection` and outputs a string containing the TypeScript representation.

In the `replacesSymbolsWithFullyQualifiedIdentifiers` method, a boolean is returned that indicates whether to use fully qualified identifiers when replacing symbols or not.

The `TypeDefinitionWriter` uses fully qualified identifiers with a namespace, whereas the `ModuleWriter` doesn't.
