Skip to content

default param

awekrx edited this page May 29, 2026 · 1 revision

default-param

Import

import { defaultParam } from '@dev-suite/decorators/default-param'

Category

  • parameter

Use Case

Provide default value when parameter is undefined/missing.

Replaces

  • x ?? default everywhere
  • Copy-pasted fallback guards

Example 1

Without decorator

class ReportApi {
  list(pageSize?: number) {
    const size = pageSize ?? 50;
    return this.repo.list(size);
  }
}

With decorator

import { defaultParam } from '@dev-suite/decorators/default-param';

class ReportApi {
  list(@defaultParam(50) pageSize: number) {
    return this.repo.list(pageSize);
  }
}

Why better

  • Centralizes cross-cutting behavior.
  • Method/class/property code stays focused on domain logic.

Example 2

Without decorator

class SearchApi {
  query(q: string, lang?: string) {
    const locale = lang ?? 'en';
    return this.engine.query(q, locale);
  }
}

With decorator

import { defaultParam } from '@dev-suite/decorators/default-param';

class SearchApi {
  query(q: string, @defaultParam('en') lang: string) {
    return this.engine.query(q, lang);
  }
}

Why better

  • Second scenario reuses same policy without duplication.
  • Behavior is more consistent and easier to audit.

Clone this wiki locally