Skip to content

Re-add strict concat signature #9997

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

Merged
merged 2 commits into from
Aug 2, 2016
Merged

Conversation

sandersn
Copy link
Member

Fixes #9901. Broken by #6629 after we missed the bug during the discussion in #6594.
#6629 introduces a new type for concat: (...items: (T | T[])[]): T[]. This means that concat accepts either list-wrapped arguments (T[]) or top-level arguments (T):

numbers.concat(1, [2, 3], 4)

Here, (1) and (4) are top-level arguments (T) whereas (2) and (3) are wrapped-arguments. Unfortunately, when you have a tuple or list type parameter, it's ambiguous whether an argument is list-wrapped or top-level:

pairs.concat([[1, 2], [3, 4]]);

If [[1,2], [3, 4]] is a top-level argument, then its type is number[][] (or [number, number][] or [[number, number], [number, number]]). If it's a list-wrapped argument, then its type is number[] (or [number, number]). But TypeScript can't tell, so it has to guess. Unfortunately, it guesses "top-level argument" (T) with type number[][]/[number, number][]. In the #9901 repro, number[][] is not compatible with the type argument T=[number, number].

Adding a stricter overload first guides the inference process to prefer the list-wrapped-only case (T[] only) before falling back to the mixed top-level/list-wrapped overload.

@weswigham
Copy link
Member

👍

@sandersn sandersn merged commit 4a470bd into master Aug 2, 2016
@weswigham weswigham deleted the re-add-strict-concat-signature branch August 11, 2016 01:44
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants