You are viewing the documentation for an older version of this package. You can check the version you are using with the following command:
composer show spatie/typescript-transformer
Selecting classes using collectors
In some cases, you'll want to transform classes without the @typescript annotation. For example Laravel's API resources are almost always sent to the front and should always have a TypeScript defintion ready to be used.
Collectors allow you to specify what PHP classes should be transformed to TypeScript and what transformer should be used.
The package comes out of the box with the pre-configured AnnotationsCollector to find and transform classes marked with the @typescript annotation.
A collector is any class that extends the Collector class and implements the shouldCollection and getClassOccurrence methods:
classEnumCollectorextendsCollector
{
publicfunctionshouldCollect(ReflectionClass $class): bool
{
// Can this collector collect this type?
}
publicfunctiongetClassOccurrence(ReflectionClass $class): ClassOccurrence
{
// Get the `ClassOccurrence` with a Transformer and name for the type
}
}
First, you have to check if the class can be collected by this collector in the shouldCollect method. When you can collect the class, getClassOccurence should return a correct ClassOccurence. The ClassOccurence contains the type's name and what transformer to use for the class.
Collectors are checked in the same order they're defined in the configuration. The package will add the AnnotationsCollector, which collects @typescript annotated classes automatically at the end. This always you to overwrite this behaviour by adding your own version of the AnnotationsCollector.