Backslashes are removed from enum values when using ::class notation.
Closed this issue · 0 comments
Bloemendaal commented
I've got the following situation. I want to map the enum values to a class-string so I can use them for my polymorphic relation and in the modal to cast them to a human readable enum.
<?php
namespace App\Enums\Pokemon;
use App\Models\Pokemon\PokemonEncounter;
use App\Models\Pokemon\PokemonEncounterMove;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
#[TypeScript()]
enum BattleRoundActionType: string
{
case SwapPokemon = PokemonEncounter::class;
case ExecuteMove = PokemonEncounterMove::class;
}
Then later in the model, I can do the following:
<?php
namespace App\Models\Pokemon;
use App\Enums\Pokemon\BattleRoundActionType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class BattleRound extends Model
{
protected $casts = [
'initiator_action_id' => 'int',
'initiator_action_type' => BattleRoundActionType::class,
];
public function initiatorAction(): MorphTo
{
return $this->morphTo();
}
}
However, when running the TypeScript generator, this results in the following code:
declare namespace App.Enums.Pokemon {
export type BattleRoundActionType =
| 'AppModelsPokemonPokemonEncounter'
| 'AppModelsPokemonPokemonEncounterMove';
}
This is what I expected:
declare namespace App.Enums.Pokemon {
export type BattleRoundActionType =
| 'App\\Models\\Pokemon\\PokemonEncounter';
| 'App\\Models\\Pokemon\\PokemonEncounterMove';
}
Running:
- PHP 8.2 on Mac
- Package version 2.3.2
Update
I found out that it is Prettier that removes the backslashes because they have no use. Without Prettier, the result looks like this:
declare namespace App.Enums.Pokemon {
export type BattleRoundActionType = 'App\Models\Pokemon\PokemonEncounter' | 'App\Models\Pokemon\PokemonEncounterMove';
}
Still not what I expected, the backslashes should be double, but it explains why there were no backslashes at all.