RobinHerbots / Inputmask

Input Mask plugin
https://robinherbots.github.io/Inputmask/
MIT License
6.36k stars 2.18k forks source link

Inputmask.format() allowed value types #2759

Closed Techn1c4l closed 2 months ago

Techn1c4l commented 7 months ago

This is probably not a bug, more likely a type definition issue, but I just wanted to be sure.

Given following settings, using space as a group separator and comma as a decimal separator:

options = {
    allowMinus: true,
    inputType: 'text',
    rightAlign: false,
    digits: 9,
    groupSeparator: ' ',
    noValuePatching: true,
    placeholder: '',
    radixPoint: ',',
    SetMaxOnOverflow: true,
    substituteRadixPoint: false,
    unmaskAsNumber: true
  };

When trying to format '1234.5' using code like this:

let value: number = 1234.5;

// According to the type definition, value must be a string, number types are not allowed

let formatted: string = Inputmask.default.format(String(value), options);
// Returns '12 345', expected '1 234,5'. Probably because of string conversion.

// Workaround to retain a number type
formatted = Inputmask.default.format(value as unknown as string, options);
// Returns '1 234,5' as expected

Is this an intended behavior (which is most likely) and a type definition issue? Or the value argument must be a string?

RobinHerbots commented 2 months ago

@Techn1c4l,

(https://robinherbots.github.io/Inputmask/#/documentation/numeric#setting-initial-values)

String(value) passes the value as "1234.5" value as unknown as string keeps the value as a number. 1234.5