0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.