1

I have a const inherited from another component that looking at its typeof, is a string. In javascript my code looks like this:

Here I display a number, eg 24%

{parseInt(progression * 100, 10)}%

Here I display a progress bar

<Bar style={{ width: `${progression * 100}%` }} />

I'm trying to change this code now to typescript and I did the following:

I declared the typing:

interface ProgressionProps {
  progression: string;
}

Here where I should display the %, I put it like this:

{Number(progression * 100, 10)}%

But I have the error:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts

And also in the other code:

<Bar style={{ width: `${progression * 100}%` }} />

I got the same error:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts

What am I doing wrong? What is the way to pass this code correctly to typescript?

Thanks if can someone help me!!

2
  • 2
    You can't operate on a string safely, so you have to convert it and only it to a string first: Number(progression). Commented Sep 14, 2022 at 16:28
  • To explain Kelly's comment in a different way, when you do progression * 100 you're trying to multiply a string by 100 and that's what Typescript is warning you about. Commented Sep 14, 2022 at 16:37

1 Answer 1

5

You need to parse the string to a number before you multiply it:

{Number(progression) * 100}%

And

<Bar style={{ width: `${Number(progression) * 100}%` }} />
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Now, with {Number(progression, 10) * 100}% I got this: Expected 0-1 arguments, but got 2.ts
Right, just Number(....) Without the ,10

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.