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: Getting started weight: 1

Until now we've only taken a look at transforming PHP classes to TypeScript, but what if you want to transform something else? This is where the TransformedProvider comes into play, it is a class that provides TypeScript types and other structures. The transformers we've seen before are actually bundled in a default TransformerProvider provided by the package.

A TransformedProvider implements the TransformedProvider interface:

namespace Spatie\TypeScriptTransformer\TransformedProviders;

interface TransformedProvider
{
    /**
     * [@return](https://github.com/return) array<Transformed>
     */
    public function provide(): array;
}

The provide method is called when the TypeScript transformer is executed, it should return Transformed objects.

We could for example add a generic type which transforms Laravel collections:

class AddLaravelCollectionProvider implements TransformedProvider
{
    public function provide(): array
    {
        $type = new Transformed(
            typeScriptNode: new TypeScriptAlias(
                new TypeScriptGeneric(
                    new TypeScriptIdentifier('Collection'),
                    [new TypeScriptIdentifier('T')],
                ),
                new TypeScriptGeneric(
                    new TypeScriptIdentifier('Array'),
                    [new TypeScriptIdentifier('T')],
                ),
            ),
            reference: new ClassStringReference(Collection::class),
            location: ['Illuminate', 'Support']
        ));

        return [$type];
    }
}

When we register the provider as such in the configuration:

$config->provider(new AddLaravelCollectionProvider());

Our transformed TypeScript will have the following type:

namespace Illuminate.Support {
    export type Collection<T> = Array<T>;
}

When referencing a Laravel collection in one of our PHP classes like this:

class Data
{
    /** [@var](https://github.com/var) Collection<string>  */
    public Collection $collection;
}

The transformed TypeScript will look like this:

export type Data = {
    collection: Illuminate.Support.Collection<string>;
}
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