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: Replacing common types weight: 5

Some PHP classes should be transformed into something atypical, an example of this is the DateTime class. When you send such an object to the front it will be represented by a string rather than an object. TypeScript transformer allows you to replace these kinds types with an appropriate TypeScript type.

Replacing types can be done in the config:

$config->replaceType(DateTime::class, 'string');

Now all DateTime objects will be transformed to a string in TypeScript. This also includes inherited classes like Carbon, those will also be transformed to a string.

When using an interface like DateTimeInterface you can also replace it with a TypeScript type:

$config->replaceType(DateTimeInterface::class, 'string');

All classes that implement DateTimeInterface will be transformed to a string in TypeScript.

Replacements

As we've seen before it is possible to replace types by writing them out like you would do in an annotation, this allows you to build complex types, for example:

$config->replaceType(DateTimeInterface::class, 'array{day: int, month: int, year: int}');

From now on, all DateTimeInterface objects will be replaced by the following TypeScript object:

{
    day: number;
    month: number;
    year: number;
}

It is also possible to define a replacement as an internal TypeScript node(more on that later):

$config->replaceType(DateTimeInterface::class, new TypeScriptString());

Or use a closure to define the replacement:

use Spatie\TypeScriptTransformer\TypeScriptNodes\TypeScriptReference;

$config->replaceType(DateTimeInterface::class, function (TypeScriptReference $reference) {
    return new TypeScriptString();
});
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