1

I am trying to convert a string array to a number array A quick google search have lead me to this solution.

    let numbersAsStringArray = originalQueryParams[property] ?? []
    let numbers = numbersAsStringArray.map((i) => Number(i));

where I keep getting this i

Property 'map' does not exist on type 'string | string[]'.

numbersAsStringArray is just an simple array with a number in as a string.

1
  • It seems the type of originalQueryParams[property] is not an array or at least typescript is not sure about it. You need to cast if you are sure it is an array or use a type guard. Commented May 19, 2022 at 9:52

2 Answers 2

3

It's a compilation error, not runtime error. Just tell TS that numberAsStringArray is of type string[] and it should be ok.

let numbersAsStringArray = (originalQueryParams[property] || []) as string[];
let numbers = numbersAsStringArray.map((i) => Number(i));
Sign up to request clarification or add additional context in comments.

Comments

0

You can try let numbers = Array.from(numbersAsStringArray,Number)

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.