Why doesn't the given code compile, and how do I fix it:
function f(x: string[] | string[][]): string[][] {
return Array.isArray(x[0]) ? x : [x];
}
As far as I can tell, the return value will always be string[][] since of x is string[], x[0] will not be an array, and if x is string[][] then x[0] will be an array.
Even though, typescript doesn't recognize this and throws an array.
Is there a way to get around it without casting? How?