1
const arr: number[] = []
const func = (n: number): void => {
    console.log(n)
}
arr.push(222)
if (arr.length > 0) {
    func(arr.shift())
}

I want to invoke func with a number as argument. However, I got this error message in IDE:

Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'.ts(2345)

How can I resolve this problem without setting the parameter form n: number to n: number | undefined?

zzzgoo
  • 1,974
  • 2
  • 17
  • 34
  • 1
    Since you already have an undefined check: `func(arr.shift()!)` – Andrew Li May 04 '19 at 01:37
  • 1
    FMI see https://stackoverflow.com/q/42273853 – Andrew Li May 04 '19 at 01:38
  • Possible duplicate of [In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?](https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci) (I thought to elaborate an answer, but it's just rehashing this question...) – msanford May 05 '19 at 00:51

0 Answers0