Functions

suggest change

Annotate parameters and exported return types. Optional parameters use ?; defaults make parameters optional to callers.

type Formatter = (value: number) => string;

function formatPrice(
  amount: number,
  currency = "USD",
  format: Formatter = String,
): string {
  return `${currency} ${format(amount)}`;
}

const label = formatPrice(42, "EUR", value => value.toFixed(2));
void label;

Use overloads only when the relationship between inputs and outputs cannot be expressed clearly with a union or generic type. TypeScript checks callback parameters according to the configured variance rules. Do not type a callback parameter more narrowly than the values it may be called with.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents