spatie/laravel-typescript-transformer

Transform your PHP structures to TypeScript types

2.4.0 2024-02-16 12:06 UTC

This package is auto-updated.

Last update: 2024-03-16 12:22:58 UTC


README

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31

Transform PHP types to TypeScript

Latest Version on Packagist GitHub Tests Action Status Styling Psalm Total Downloads

Always wanted type safety within PHP and TypeScript without duplicating a lot of code? Then you will like this package! Let's say you have an enum:

class Languages extends Enum
{
    const TYPESCRIPT = 'typescript';
    const PHP = 'php';
}

Wouldn't it be cool if you could have an automatically generated TypeScript definition like this:

export type Languages = 'typescript' | 'php';

This package will automatically generate such definitions for you, the only thing you have to do is adding this annotation:

/** @typescript **/
class Languages extends Enum
{
    const TYPESCRIPT = 'typescript';
    const PHP = 'php';
}

You can even take it a bit further and generate TypeScript from classes:

/** @typescript */
class User
{
    public int $id;

    public string $name;

    public ?string $address;
}

This will be transformed to:

export type User = {
    id: number;
    name: string;
    address: string | null;
}

Want to know more? You can find the documentation here.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.