0
r = Math.floor(Math.random() * arrOfCourse.length) + 1;
if (arr.indexOf(r) === -1) arr.push(r);

I got this error on the r Argument of type 'number' is not assignable to parameter of type 'never'.

enter image description here

I'm suppose to cast r ? If I try to do: r:any = Math.floor(Math.random() * arrOfCourse.length) + 1; nothing change...

samuel
  • 477
  • 7
  • 21
  • 4
    `var arr: number[] = []` should resolve it. – Brian Lee Nov 25 '20 at 09:52
  • @DigitalDrifter thanks this is working! But I don't understand why? – samuel Nov 25 '20 at 09:54
  • Ts wasn't able to infer the type of elements the array would contain, so it uses the `never` type. – Brian Lee Nov 25 '20 at 09:56
  • 1
    because a non typed array defaults to the type never. This in turn forces you to strictly define types for variables. How else would typescript know that the array shouldn't contain strings? – Kilian Nov 25 '20 at 09:57

0 Answers0