After all, which the difference between string[] | number[] and (string | number)[]?

Advertisements

I was looking for some codes on github and I found some interfaces with

interface A {
  type: string[] | number[]
}

and others like

interface A {
  type: (string | number)[]
}

But, whitch the difference?

>Solution :

The first is "an array of strings or an array of numbers". In which the array can contain strings or numbers, but not a mix of both.

The second is "an array of strings or numbers". In which the array can contain strings, numbers, or a mix of both.

Consider them structurally as:

(an array of strings) or (an array of numbers)

and

an array of (strings or numbers)

Leave a ReplyCancel reply