Skip to content

Enum support#13

Merged
mxaGianluca merged 3 commits intovagebnd:mainfrom
rick-bongers:enum-support
Jan 16, 2026
Merged

Enum support#13
mxaGianluca merged 3 commits intovagebnd:mainfrom
rick-bongers:enum-support

Conversation

@rick-bongers
Copy link
Contributor

This pull request enhances enum type handling in the Types class to improve how PHP enums are converted for TypeScript type generation. The most important changes are the addition of support for BackedEnum instances and a new method for processing enums.

Enum type handling improvements:

  • Updated the determineType method in src/Types/Types.php to handle values that are instances of \BackedEnum, delegating their processing to the new processEnum method.
  • Added a new private method processEnum in src/Types/Types.php that generates a TypeScript union type string from all possible enum values, properly quoting string values.

@mikemeijer
Copy link

Can you extend the tests?

@Lukasss93
Copy link

any news about this?

@rick-bongers
Copy link
Contributor Author

Hi @mxaGianluca, I’d love to contribute more to this package, but I noticed it has been a bit quiet lately. Are there plans to actively maintain this project going forward? I'm happy to help out where I can.

@mxaGianluca
Copy link
Contributor

Hi @rick-bongers thank you for this PR. I've been too busy and haven't checked it out yet. But I do intend on maintaining this package.

I'll get back to you once I've checked it out.

@mxaGianluca
Copy link
Contributor

Hey @rick-bongers i've just looked at your commits and I want to merge them but the tests are failing. The assertions aren't correct.

Tests\Types\TypesTest > it can determine a type from a value with (Vagebond\Runtype\Tests\Fakes\Enums\TestEnum…    
  Failed asserting that two strings are identical.
  -'enum-value-1 | enum-value-2'
  +''enum-value-1' | 'enum-value-2''
  

  at tests/Types/TypesTest.php:9
      5use Vagebond\Runtype\Tests\Fakes\Resources\ProductResource;
      6use Vagebond\Runtype\Types\Types;
      78it('can determine a type from a value', function ($value, $expected) {
  ➜   9expect((new Types(getConfig()))->determineType($value))->toBe($expected);
     10▕ })->with([
     11▕     ['string', 'string'],
     12▕     [true, 'boolean'],
     13▕     [1, 'number'],

  1   tests/Types/TypesTest.php:9

@rick-bongers
Copy link
Contributor Author

@mxaGianluca my mistake! I've fixed the tests

@mxaGianluca mxaGianluca merged commit e3014ac into vagebnd:main Jan 16, 2026
1 check passed
@mxaGianluca
Copy link
Contributor

Thank you for your contribution @rick-bongers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants