Home > Net >  Type 'number' is not assignable to type '[string | number, string]'
Type 'number' is not assignable to type '[string | number, string]'

Time:03-12

I got this error, when i try to format the currency value.

type CurrencyState = {
    ...,
    initialValue: [number | string, string];
}

let formatedValue = this.state.initialValue;

        if (typeof formatedValue === 'string'){
            formatedValue = parseInt(formatedValue)
        }
        formatedValue = Intl.NumberFormat("en", { style: "decimal", minimumFractionDigits: 2 }).format(formatedValue );

CodePudding user response:

The way you defined initialValue's type means that it has to be an array, where its first value is either a number or a string, and its second value is a string.

In your code you check if initialValue is a string, which it can never be.

So either remove the brackets from the type, to make it number | string, or treat initialValue as an array of 2 values like so: typeof formatedValue[0] === 'string'

  • Related