Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in type annotation of NumberFormatBaseProps (NumberFormatBase) #740

Closed
akhmadullin opened this issue Mar 6, 2023 · 5 comments · Fixed by #756
Closed

Error in type annotation of NumberFormatBaseProps (NumberFormatBase) #740

akhmadullin opened this issue Mar 6, 2023 · 5 comments · Fixed by #756

Comments

@akhmadullin
Copy link

akhmadullin commented Mar 6, 2023

Describe the issue and the actual behavior

NumberFormatBase component's props have following type annotation:

Снимок экрана 2023-03-06 в 12 35 21

As you can see, format, removeFormatting and getCaretBoundary props are defined as required props.

But in implementation of NumberFormatBase component we can see, that these props are optional, because they have default value:

Снимок экрана 2023-03-06 в 12 47 19

Because format, removeFormatting and getCaretBoundary props are defined as required, I have following TS error*, when I try to use NumberFormatBase component with only one set prop - format:

Снимок экрана 2023-03-06 в 12 57 18

*TS error - Type '{ valueIsNumericString: true; value: string; onValueChange: (values: NumberFormatValues) => void; format: (phoneNumber: string) => string; }' is missing the following properties from type 'NumberFormatBase': removeFormatting, getCaretBoundary ts(2739)

I set only format prop, because I need only custom formatting in my case. But TS expects removeFormatting and getCaretBoundary will be set too. Despite the fact, that removeFormatting and getCaretBoundary props are missing, component works ok, because NumberFormatBase component has default values for these props.

Describe the expected behavior

I see 2 possible solutions:

  1. format, removeFormatting and getCaretBoundary props are defined as optional props in type annotation:
type NumberFormatBase = {
  format?: FormatInputValueFunction;
  removeFormatting?: RemoveFormattingFunction;
  getCaretBoundary?: (formattedValue: string) => boolean[];
};
  1. If react-number-format for some reason needs format, removeFormatting and getCaretBoundary props to be required, default values of these props should be exported too, to be used where lib users need default behavior:
// react-number-format
export { caretUnknownFormatBoundary, defaultRemoveFormatting, defaultFormat }
// some project
import { NumberFormatBase, caretUnknownFormatBoundary, defaultRemoveFormatting  } from 'react-number-format';

const customFormatter = () => {...};

...

return (
    <NumberFormatBase 
        format={customFormatter}
        removeFormatting={defaultRemoveFormatting}
        getCaretBoundary={caretUnknownFormatBoundary}
    />
)

Provide a CodeSandbox link illustrating the issue

Modified demo from your docs - https://codesandbox.io/s/card-expiry-field-forked-8ex966?file=/src/App.tsx

@danielweil
Copy link

UPVOTE

@s-yadav
Copy link
Owner

s-yadav commented May 7, 2023

Yes, make sense. Will fix the annotation here.

s-yadav added a commit that referenced this issue May 10, 2023
s-yadav added a commit that referenced this issue May 14, 2023
@s-yadav
Copy link
Owner

s-yadav commented May 14, 2023

This is fixed on v5.2.0

@frindly
Copy link

frindly commented May 16, 2023

This produces a breaking change in typescript for the useNumericFormat hook

Consider:

const { format, removeFormatting } = useNumericFormat(NumericFormatCurrencyProps);
...
format(val);

This update causes these newly optional properties to be returned as FormatInputValueFunction | undefined, so typescript will now complain with TS2722: Cannot invoke an object which is possibly 'undefined'., Is this hook returning undefined fns for these values actually possible?

@TheAsda
Copy link

TheAsda commented Apr 24, 2024

Facing the same issue

This produces a breaking change in typescript for the useNumericFormat hook

Consider:

const { format, removeFormatting } = useNumericFormat(NumericFormatCurrencyProps);
...
format(val);

This update causes these newly optional properties to be returned as FormatInputValueFunction | undefined, so typescript will now complain with TS2722: Cannot invoke an object which is possibly 'undefined'., Is this hook returning undefined fns for these values actually possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants