Closed
Description
TypeScript Version: 4.2.0-dev.20201211
Search Terms:
re: Array.isArray
Code
interface TestCase<T extends string | number> {
readonly val1: T | ReadonlyArray<T>;
readonly val2: ReadonlyArray<T>;
}
interface MultiCaseFixture<T> {
cases: T[];
}
function subDataFunc(): TestCase<string | number>[] {
return [
{ val1: "a", val2: ["a", "b", "c"] },
{ val1: 2, val2: [1, 2, 3] },
{ val1: ["a", "z"], val2: ["x", "y", "z"] },
{ val1: [5, 10], val2: [10, 100, 1000] },
];
}
function dataFunc<T>(subFunc: () => T[]): MultiCaseFixture<T> {
return { cases: subFunc() };
}
function testFunc() {
const fixture = dataFunc<TestCase<string | number>>(subDataFunc);
fixture.cases.forEach(({ val1, val2 }) => {
if (Array.isArray(val1)) {
const reversedVal1 = val1.slice().reverse();
console.log(reversedVal1);
} else {
console.log(val1);
}
console.log(val2);
});
}
testFunc();
Expected behavior:
This code should compile properly without issues. It compiles fine on 4.1.2
and also compiled fine on previous releases going all the way back to at least 3.8
.
Actual behavior:
I get this output from tsc
with the command-line yarn run tsc test.ts
. There is no tsconfig.json
file in the folder at the time.
test.ts:27:47 - error TS2339: Property 'reverse' does not exist on type 'string | any[]'.
Property 'reverse' does not exist on type 'string'.
27 const reversedVal1 = val1.slice().reverse();
~~~~~~~
Found 1 error.
Related Issues:
According to #40463 and #41808, an issue with Array.isArray
has already been fixed and is supposedly in 4.1.3
.