0

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 );
2
  • 2
    I guess the type should just be number | string, not [number | string, string]. Commented Mar 11, 2022 at 12:10
  • initialValue is array but you try to set number Commented Mar 11, 2022 at 12:11

1 Answer 1

1

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'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.