Skip to content

Add overloads for 'map' on tuple types #11252

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 5 commits into from
Oct 4, 2016
Merged

Conversation

DanielRosenwasser
Copy link
Member

This PR fixes #6574.

Sometimes users have the expectation that calling map on a homogeneous tuple will create a tuple type of the same size, but with the element types appropriately mapped on. For instance:

interface Person {
    name: string;
}

declare let couple: [Person, Person];

// Should have type '[string, string]', but currently has type 'string[]'.
let names = couple.map(p => p.name);

Now that we have this-types, we can actually enable overloads that can only apply if the target object (the thing being dotted on) has a specific type.

I'd like to hear feedback from the team (especially @sandersn & @ahejlsberg) about whether this is a good idea. This fulfills a lot of users' expectations, but they could instead just augment Array<T> with these overloads if they're interested.

Also tagging @philpee2 who asked me about this yesterday.

Copy link
Contributor

@mhegazy mhegazy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 after fixing failing tests

@sandersn
Copy link
Member

sandersn commented Sep 29, 2016

This is specialised for [T, T] tuples, which aren't that common. In #6574 @aluanhaddad makes two good points:

  1. T[] isn't that much worse than [T, T] as the result of map unless
  2. [T, T] is a commonly used structure, in which case a tuple is not that good of a representation for it.

Can't individuals augment the Array interface themselves if they want this type for map?

@DanielRosenwasser DanielRosenwasser merged commit a0fc905 into master Oct 4, 2016
@DanielRosenwasser DanielRosenwasser deleted the mapOnTuples branch October 4, 2016 03:07
@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.

Tuple types and map
4 participants