PHPackages                             bolzer/ts-const-enum - 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. bolzer/ts-const-enum

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

bolzer/ts-const-enum
====================

A command line tool to generate typescript constants from your php constants

0.2(4y ago)04Apache-2.0PHPPHP &gt;=8.0

Since Jul 27Pushed 4y ago1 watchersCompare

[ Source](https://github.com/BolZer/ts-const-enum)[ Packagist](https://packagist.org/packages/bolzer/ts-const-enum)[ RSS](/packages/bolzer-ts-const-enum/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Ts-Const-Enum (PHP)
===================

[](#ts-const-enum-php)

[![maintained](https://camo.githubusercontent.com/ff35e0c957ea98e571fb33d6f51d48cc16ec3d6fe68e83b60fe11dc0ec832a60/68747470733a2f2f696d672e736869656c64732e696f2f6d61696e74656e616e63652f7965732f323032313f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/ff35e0c957ea98e571fb33d6f51d48cc16ec3d6fe68e83b60fe11dc0ec832a60/68747470733a2f2f696d672e736869656c64732e696f2f6d61696e74656e616e63652f7965732f323032313f7374796c653d666f722d7468652d6261646765)

Description
-----------

[](#description)

This tool will transform PHP Scalar-Constants - which are annotated with the provided attributes - to Typescript Constants and generates Typescript Enums from annotated PHP one dimensional constant arrays. This may be useful if you want to reference a value in a condition within your Typescript Code. You can easily import the generated constants. This has many advantages over just stating the value. If the value of the constant changes, your code won't break easily. If you change the name of a php constant the typescript code won't compile unless you changed all occurrences of the old constant name.

This is especially useful if you use alot of JS/TS-Frameworks and do conditionally rendering in regards to values / properties. The Constants are by default prefixed with the declaring class followed by \_\_ and the constant name. You may provide an alias on attribute-level. Keep in mind to have unique names / alias as the constants will land in one file.

### Usecase

[](#usecase)

#### Before

[](#before)

```
if (reflection.type === 16) {
    // Do something when true
}
```

### After

[](#after)

```
import {TARGET_CLASS_CONSTANT} from "./constants";

if (reflection.type === TARGET_CLASS_CONSTANT) {
    // Do something when true
}
```

Installation
============

[](#installation)

1. Require the dependency

```
composer require bolzer/ts-const-enum
```

2. Create in your root (where the vendor folder is) dir a config file

```
touch .ts-const-enum-config.php
```

3. Add the following content to the previously created file with the output path.

```
