PHPackages                             paneon/php-to-typescript - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. paneon/php-to-typescript

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

paneon/php-to-typescript
========================

Generate TypeScript classes and interfaces based on PHP classes.

v2.3.0(2mo ago)311.3k↓100%1MITPHPCI passing

Since Dec 10Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Paneon/php-to-typescript)[ Packagist](https://packagist.org/packages/paneon/php-to-typescript)[ RSS](/packages/paneon-php-to-typescript/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (11)Used By (1)

PHP To TypeScript Parser
========================

[](#php-to-typescript-parser)

[![Build](https://github.com/Paneon/php-to-typescript/actions/workflows/main.yml/badge.svg)](https://github.com/Paneon/php-to-typescript/actions/workflows/main.yml)[![Latest Stable Version](https://camo.githubusercontent.com/429405d9d801ba26ca2086043067898bed250c2772732ead2999479e08bd6fd0/68747470733a2f2f706f7365722e707567782e6f72672f70616e656f6e2f7068702d746f2d747970657363726970742f762f737461626c65)](https://packagist.org/packages/paneon/php-to-typescript)[![Total Downloads](https://camo.githubusercontent.com/cffb2a6203bf11b9a7d2fa8ce3477a4aa0a5638e8a26c8f9561fd9d24ebaf631/68747470733a2f2f706f7365722e707567782e6f72672f70616e656f6e2f7068702d746f2d747970657363726970742f646f776e6c6f616473)](https://packagist.org/packages/paneon/php-to-typescript)[![License](https://camo.githubusercontent.com/cde5437ace4b147a8a697a14fab8675111c888331d1286a1eea0075d2a2a6a63/68747470733a2f2f706f7365722e707567782e6f72672f70616e656f6e2f7068702d746f2d747970657363726970742f6c6963656e7365)](https://packagist.org/packages/paneon/php-to-typescript)

A library which can be used to create TypeScript classes/interfaces based on PHP classes and enums. Main use case is in a scenario where a PHP backend is consumed by a JavaScript/TypeScript frontend and serialized DTOs are consumed.

TypeScript is a superscript of JavaScript that adds strong typing and other features on top of JS. Automatically generated classes can be useful, for example when using a simple JSON API to communicate to a JavaScript client. This way you can get typing for your API responses in an easy way.

Feel free to build on this or use as inspiration to build something completely different.

Installation
------------

[](#installation)

```
$ composer require paneon/php-to-typescript
```

Features
--------

[](#features)

- Generate TypeScript interfaces from PHP classes
- Generate TypeScript enums from PHP enums (string-backed, int-backed, or unit enums)
- Optional string literal union type output for enums
- Support for nullable types
- Configurable prefix/suffix for generated type names
- Export keyword support

Classes
-------

[](#classes)

Mark your PHP classes with the `#[TypeScript]` attribute to generate TypeScript interfaces.

#### Example source file:

[](#example-source-file)

```
