Skip to content

Conversation

gvlasov
Copy link

@gvlasov gvlasov commented Jun 5, 2025

Problem:

  • When using namespaced type definitions, it uses fully qualified type names, but they are long and repetitive, they can introduce a lot of noise into frontend code. But we do need them so types with the same name from different namespaces don't clash.
  • When using data class name suffixes, they are repeated on every resulting typescript type definition. But you need them on the backend so that the class name doesn't clash with your e.g. database entity names.

Solution: introduce Compactor to remove redundant parts from type names and namespaces

Example: same entity in different bounded contexts: accounting and inventory

<?php

namespace App\Data\Accounting;

use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript]
class ProductData
{
    public function __construct(
        public int $acquisitionCost
    ) {
    }
}
<?php

namespace App\Data\Inventory;

use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript]
class ProductData
{
      public function __construct(
        public string $sku,
        public int $price
    ) {
    }
}

Before Compactor:

$ artisan typescript:transform
+--------------------------------------------+---------------------------------+
| PHP class                                  | TypeScript entity               |
+--------------------------------------------+---------------------------------+
| App\Data\Accounting\ProductData            | App.Data.Accounting.ProductData |
| App\Data\Inventory\ProductData             | App.Data.Inventory.ProductData  |
+--------------------------------------------+---------------------------------+
declare namespace App.Data.Accounting {
    export type ProductData = {
        acquisitionCost: number;    
    };
}
declare namespace App.Data.Inventory {
    export type ProductData = {
        sku: string;
        price: number;    
    };
}

With Compactor:

    // in config/typescript-transformers.php
    'compactor' => [
        'prefixes' => ['App\\Data', 'App\\Enums'],
        'suffixes' => 'Data',
    ],
$ artisan typescript:transform
+--------------------------------------------+--------------------+
| PHP class                                  | TypeScript entity  |
+--------------------------------------------+--------------------+
| App\Data\Accounting\ProductData            | Accounting.Product |
| App\Data\Accounting\Inventory\ProductData  | Inventory.Product  |
+--------------------------------------------+--------------------+

declare namespace Accounting {
    export type Product = {
        acquisitionCost: number;
    };
}
declare namespace Inventory {
    export type Product = {
        sku: string;
        price: number;
    };
}

Notes

Some things are still missing and it needs more testing.

What do you think?

@gvlasov gvlasov changed the title Introduce Compactors to shorten output namespaces and type names Introduce Compactor to shorten output namespaces and type names Jun 5, 2025
@spatie-bot
Copy link

Dear contributor,

because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it.

@spatie-bot spatie-bot closed this Oct 8, 2025
@rubenvanassche rubenvanassche reopened this Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants