Functions
suggest changeAnnotate 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents