Feature request: #[TypeScriptPick] attribute to generate Pick utility types from DTOs #62
Unanswered
valentin-morice
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi 👋
First of all I want to thank you for all the wonderful packages you're producing, my company and I use them every day. They even sent the you the postcard! This is my first feature proposal, and I think it's small enough to make it in the core. If you do not find it suitable, I can still release it as a package.
Problem
When using Laravel Data objects (or any DTO) with TypeScript, it's common to return partial representations from API endpoints. For example, a
UserDataclass might have 15 properties, but a list endpoint only returnsidandname.Currently, the options are:
#[Hidden]— but this is destructive (removes from the main type) and not reusable across variants#[Optional]at class level — makes everything optional (equivalent toPartial<T>), which is too broadTypeScript already has the perfect tool for this:
Pick<T, K>. It would be ideal if the transformer could generate these automatically.Proposed Solution
A new
#[TypeScriptPick]attribute (or#[TypeScriptPicks]for multiple) that generates named Pick types alongside the main DTO type:Generated TypeScript:
Usage in Front-end
Implementation Notes
The attribute could be implemented as a Repeatable attribute targeting classes:
On the transformer side, each
#[TypeScriptPick]would produce an additional Transformed object containing:This means the transformer (or a dedicated
TransformedProvider) would need to emit multiple Transformed objects per class, one for the base type, plus one per#[TypeScriptPick].Alternatives Considered
#[TypeScriptOmit]— the inverse (Omit<UserData, 'phone' | 'address'>) could also be useful, especially when you want most properties. Could be a companion attribute.#[TypeScriptPicks(['List' => ['id', 'name'], 'Card' => [...]])]— more compact but less readable than repeatable attributes.Why This Matters
This is a common pain point in Laravel + TypeScript projects, at least for me. TypeScript's utility types exist precisely for this purpose — the transformer could leverage them.
Beta Was this translation helpful? Give feedback.
All reactions