-
Notifications
You must be signed in to change notification settings - Fork 0
coerce
awekrx edited this page May 29, 2026
·
1 revision
import { coerce } from '@dev-suite/decorators/coerce'
parameter
Convert incoming parameter type (string -> number/boolean/date/etc.).
- Inline
Number()/Boolean()conversions - Duplicated parsing in controllers
class BillingController {
list(limit: unknown) {
const n = Number(limit);
return this.service.list(n);
}
}import { coerce } from '@dev-suite/decorators/coerce';
class BillingController {
list(@coerce({ to: 'number' }) limit: number) {
return this.service.list(limit);
}
}- Centralizes cross-cutting behavior.
- Method/class/property code stays focused on domain logic.
class FeatureController {
toggle(enabled: unknown) {
const flag = enabled === 'true' || enabled === true;
return this.service.toggle(flag);
}
}import { coerce } from '@dev-suite/decorators/coerce';
class FeatureController {
toggle(@coerce({ to: 'boolean' }) enabled: boolean) {
return this.service.toggle(enabled);
}
}- Second scenario reuses same policy without duplication.
- Behavior is more consistent and easier to audit.