From ca4315f604df347a1b81c658eac1a7cc237bac11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20R=C3=B8en?= Date: Mon, 22 Jan 2024 21:52:05 +0100 Subject: [PATCH] feat(NumberUtils.format): Only return object if returnAria: true (#3262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../src/components/number-format/NumberUtils.d.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/dnb-eufemia/src/components/number-format/NumberUtils.d.ts b/packages/dnb-eufemia/src/components/number-format/NumberUtils.d.ts index 01bbbfe3197..1774d0ca211 100644 --- a/packages/dnb-eufemia/src/components/number-format/NumberUtils.d.ts +++ b/packages/dnb-eufemia/src/components/number-format/NumberUtils.d.ts @@ -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; @@ -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;