const s: string = 'foo';
const pass1 = (origin: string) => origin.concat(s);
const pass2 = (origin: string[]) => origin.concat(s);
type S = string | string[];
const error = (origin: S) => origin.concat(s);
In the code snippet above, I am able to call concat
on both a string
and a string[]
. However, TypeScript prohibits calling concat
on a string | string[]
type.
The error message states:
Cannot invoke an expression whose type lacks a call signature.
Type '((...strings: string[]) => string) | { (...items: ConcatArray<string>[]): string[]; (...items: (s...'
has no compatible call signatures.
This could be due to the different return types. Even though I believe TypeScript should be able to infer that the type of error
is S
, it seems to be an intentional design choice. But why?