spatie/typescript-transformer

Generating Record-like types

Closed this issue · 1 comments

I can't seem to find a way to generate a type like the following:

type FleetDetails = Record<Domain.Operations.Enums.AircraftType, Domain.Operations.Data.AircraftData[]>

I had to resort to something like that:

class FleetRadarData extends Data
{
    public function __construct(
        #[LiteralTypeScriptType('Record<Domain.Operations.Enums.AircraftType, Domain.Operations.Data.AircraftData[]>')]
        public readonly array $fleet,
        // ...
    ) {
    }
}

I don't think it's ideal because the string is not refactor-proof. I would love to be able to have an API for this using actual FQCNs, but I have no clue how I could describe the value part of the record being an array. For a simple key-value record, we could have something like this:

#[TypeScriptRecord(AircraftType::class, AircraftData::class)]

Maybe the solution would be to have a AircraftDataCollection class but I don't know how it could be typed without adding an attribute for it and specific support.

Any input?

Yeah I would create such a custom attribute take a look at the interface which LiteralTypeScriptType implements to see how it can be done.