Skip to content

Introduce Compactor to shorten output namespaces and type names #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

1 participant