PHP enums still not transformed correctly
Closed this issue · 2 comments
iamrgroot commented
Using the following versions:
"spatie/laravel-data": "^1.2.4",
"spatie/laravel-typescript-transformer": "^2.1.3",
"spatie/typescript-transformer": "^2.1.6"
The following Data object:
#[TypeScript]
class Test extends Data
{
public function __construct(
public WorkOrderType $type = WorkOrderType::UNKNOWN,
) {
}
}
enum WorkOrderType: string
{
case UNKNOWN = 'UNKNOWN;
}
Gets transformed to any:
export type Test = {
type: any;
};
Am I missing something in setup or config that causes this?
My work around is adding the following processor. Maybe this could be (improved and) included by default?
class ConvertEnumTypeProcessor implements TypeProcessor
{
public function process(
Type $type,
ReflectionParameter | ReflectionMethod | ReflectionProperty $reflection,
MissingSymbolsCollection $missingSymbolsCollection
): ?Type {
if ($type instanceof Object_ && enum_exists($type->__toString())) {
$backing_type = (new ReflectionEnum($type->__toString()))->getBackingType();
return $backing_type ? (new TypeResolver())->resolve((string) $backing_type) : $type;
}
return $type;
}
}
rovansteen commented
You are probably missing the typescript annotation on your Enum.
iamrgroot commented
@rovansteen thanks for the suggestion!
In the end I was missing two things I assumed where done automatically:
#[TypeScript]
annotation on my enumsSpatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
entry in thetypescript-transformer.transformers
config