Transform PHP classes and enums to TypeScript
mainThe spatie/typescript-transformer package converts PHP structures like classes and enums into TypeScript types.
To mark a class for transformation, use the #[TypeScript] attribute. The transformer maps PHP types to their TypeScript equivalents (e.g., int to number, nullable types to unions with null).
For string-backed enums, the transformer generates a TypeScript union type of the enum values.
#[TypeScript]
class User
{
public int $id;
public string $name;
public ?string $address;
}Converts to:
export type User = {
id: number;
name: string;
address: string | null;
}And enums:
enum Languages: string
{
case TYPESCRIPT = 'typescript';
case PHP = 'php';
}Converts to:
export type Languages = 'typescript' | 'php';