Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Typescript Transformer Laravel Package

spatie/typescript-transformer

Automatically generate TypeScript definitions from your PHP/Laravel code. spatie/typescript-transformer scans classes and types, then outputs .d.ts files so your frontend stays in sync with backend models, DTOs and enums with minimal manual typing.

View on GitHub
Deep Wiki
Context7

title: Enum transformer weight: 3

The package ships with a built-in EnumTransformer which can transform PHP enums to TypeScript enums or union types.

It is possible to configure this transformer to generate enums for other types of enums than the native PHP enums by implementing a custom EnumProvider:

use Spatie\TypeScriptTransformer\Transformers\EnumProviders\EnumProvider;

use Spatie\Enum\Enum;
use Spatie\TypeScriptTransformer\PhpNodes\PhpClassNode;

class SpatieEnumProvider implements EnumProvider
{
    public function isEnum(PhpClassNode $phpClassNode): bool
    {
        return $phpClassNode->reflection->isSubclassOf(Enum::class);
    }

    public function isValidUnion(PhpClassNode $phpClassNode): bool
    {
        return true;
    }

    public function resolveCases(PhpClassNode $phpClassNode): array
    {
        /** [@var](https://github.com/var) class-string<Enum> $className */
        $className = $phpClassNode->getName();

        return array_map(
            fn (Enum $enum) => [
                'name' => $enum->value,
                'value' => $enum->value,
            ],
            $className::cases()
        );
    }
}

The isEnum method should return whether the provided class node is an enum, the isValidUnion method should return whether the enum can be transformed to a union type rather than a TypeScript enum. The resolveCases method should return an array of cases for the enum which is a mapping of case names to their values.

Within your configuration you can register the transformer as such:

$config->transformer(
    new EnumTransformer(enumProvider: new SpatieEnumProvider())
);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor