-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS ^4 does not recognize array methods on (T[] | T[][]) as callable #40157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The error disappears if the array is initialized with something inside. const a: string[] | string[][] = [];
a.every(() => {}); // TS-2349: This expression is not callable.
const b: string[] | string[][] = [[]];
b.every(() => {}); // ok
const c: string[] | string[][] = ['c'];
c.every(() => {}); // ok
const f = (d: string[] | string[][]) => {
d.every(() => {}); // TS-2349: This expression is not callable.
} |
Methods, that do not have generic overloads ( |
This is working as intended (or at least a current design limitation), if a breaking change in 4.0 due to a Changing the types you annotate on the argument to |
@RyanCavanaugh up to you - this break should possibly be noted in the release breaking changes, if we want to keep it (otherwise we'd revert #38200 and unfix its associated issue). |
So is there currently any proper workaround? I can't seem to get around the error. |
If introducing runtime typeguard is acceptable (in my case - it is), you could go with something along the lines of: const isArrayOfArrays = (candidate: unknown[]): candidate is unknown[][] => {
// you might want to add special case for an empty candidate - this implementation returns `true`
return candidate.every(Array.isArray);
}
const f = (a: string[] | string[][]) => {
const g = (b: string | string[]) => {
// ...
}
if(isArrayOfArrays(a)) {
a.every(g);
} else {
a.every(g);
}
} |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 4.0.2, 4.1.0-dev.20200820
Search Terms: union, array methods
Code
The following example uses
every
method, but the bug affects all other methods as well (map
,reduce
, etc.).Expected behavior: No errors, code should compile.
Actual behavior: Error
TS-2349: This expression is not callable.
Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-dev.20200819#code/MYewdgzgLgBAhgLhtATgSzAcwNoF0YA+yU6WeeMAvDHgNwBQ9cAdAKYBurKAngBQBGVAHwxQkEABtWzCSEwCAlAoZM2nHrwFJUGTIWKkcuBcNHgIk6bPn8lKlhy58tB3fp1ljSQWgiusplDcAA6sIABmMIKUMTAA5B6Yccr0QA
Related Issues: I didn't find anything similar.
The text was updated successfully, but these errors were encountered: