Transform PHP classes and enums to TypeScript
mainThe laravel-typescript-transformer package allows you to automatically generate TypeScript types from your PHP code. By using the #[TypeScript] attribute on PHP classes, you can convert class properties into TypeScript types. It also supports converting PHP enums into TypeScript string union types.
Class Transformation
When a class is marked with #[TypeScript], its public properties are mapped to a TypeScript type definition. PHP types like int, string, and nullable types ?string are converted to their TypeScript equivalents (number, string, and string | null).
Enum Transformation
PHP enums are converted into TypeScript string union types, representing the backed values of the enum cases.
#[TypeScript]
class User
{
public int $id;
public string $name;
public ?string $address;
}// Becomes:
export type User = {
id: number;
name: string;
address: string | null;
}enum Languages: string
{
case TYPESCRIPT = 'typescript';
case PHP = 'php';
}// Becomes:
export type Languages = 'typescript' | 'php';