Skip to content

Commit

Permalink
feat(NumberUtils.format): Only return object if returnAria: true (#3262)
Browse files Browse the repository at this point in the history
The function `format` only returns an object if the option `returnAria`
was passed in as `true`, but TypeScript doesn’t know this.

With this change, we’re making an overload for the function that only
returns the object in the right situation. This way, the caller doesn’t
need to check the returned value before being able to use it as a string
(or number).

The `formatReturnType` type is now unused, but given it's been exported
I opted to not remove it as that might be considered a breaking change.

You can consider this more of a suggestion than a request though, I'm
not sure what your policy or testing setup is like for type changes :)

Co-authored-by: Kim Røen <kim.frostad.roen@dnb.no>
  • Loading branch information
kimroen and Kim Røen authored Jan 22, 2024
1 parent 0912bb7 commit ca4315f
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface formatReturnValue {
}
export type formatValue = string | number;
export type formatReturnType = formatReturnValue | formatValue;

export interface formatOptionParams {
/** can be "auto" */
locale?: Locale;
Expand Down Expand Up @@ -80,10 +81,15 @@ export interface formatOptionParams {
/** If an object should be returned, including the "aria" property */
returnAria?: boolean;
}
export const format: (

export function format(
value: formatValue,
options: formatOptionParams & { returnAria: true }
): formatReturnValue;
export function format(
value: formatValue,
options?: formatOptionParams
) => formatReturnType;
): formatValue;

type cleanNumberOptions = {
decimalSeparator?: string;
Expand Down

0 comments on commit ca4315f

Please sign in to comment.