Reputation: 1
Why this simple code
const arr: [string, number] = ['str', 1];
function fn(arg1?: string, arg2?: number): void {
alert(arg1);
}
fn(...arr);
produces this error:
Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'.
When I change my call to
fn(arr[0], arr[1])
everything works fine.
Why spread does not work as expected? Why type is also converted to 'string | number'? Shouldn't it detect that the first one is a string and the latter is a number?
Upvotes: 0
Views: 389
Reputation: 620
Good find. This has already been picked up as a bug by the Typescript community:
https://github.com/Microsoft/TypeScript/issues/4130
It looks like they are still working on it.
Upvotes: 1