Can someone explain are those 2 array union types identical? And if they are not the same, what would be the correct use case for each one?
const a: string[] | number[] = [];
const b: (string|number)[] = []
They are not identical. Here is the difference:
const a: string[] | number[] = [];
This means that a can be array of strings or array of numbers. So:
// Array of strings
a = ['h', 'e'];
// or Array of numbers
a = [1, 2];
const a: string|number[] = []
Means that a is either a string OR an array of numbers
// string
a = 'hello';
// or array of numbers
a = [1, 2];
const a: (string|number)[] = []
Means that a is an array where each item in the array can be a number or a string so:
// (string | number) array
a = ['h', 1];
b: (string | number)[]becausestring|number[]means either a string as a plain value or an array of numbers.