You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've implemented a custom transformer which reads PHP class docblock @property tags and generates the Typescript defintion from these, instead of actual class properties. This is useful in cases where you'd want to transform things like Eloquent models into TS defintions, since there's no simple way to type the actual Eloquent model properties.
Would you be interested in having us contribute this in a PR?
Notice how the Product class does not have any PHP types, other than the @property tags in the class docblock. Also, there are a few special docblock description tags - @ts-optional and @ts-hidden, which tell the transformer to make the property optional, or hide it from TS completely.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
We've implemented a custom transformer which reads PHP class docblock
@property
tags and generates the Typescript defintion from these, instead of actual class properties. This is useful in cases where you'd want to transform things like Eloquent models into TS defintions, since there's no simple way to type the actual Eloquent model properties.Would you be interested in having us contribute this in a PR?
Basically, given something like this:
Will get you this:
Notice how the
Product
class does not have any PHP types, other than the@property
tags in the class docblock. Also, there are a few special docblock description tags -@ts-optional
and@ts-hidden
, which tell the transformer to make the property optional, or hide it from TS completely.Using these tags it would also be possible to implement support for literal TS types and Record types.
We've also worked out a way to support unqualified class names in docblocks, so there's no need to use FQCNs.
Would this be something valuable to this package? If so, I can extract the implementation our of our codebase and prepare a PR.
Beta Was this translation helpful? Give feedback.
All reactions