Skip to content
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

Undesirable type argument inference from return position #20681

Closed
DanielRosenwasser opened this issue Dec 13, 2017 · 5 comments
Closed

Undesirable type argument inference from return position #20681

DanielRosenwasser opened this issue Dec 13, 2017 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@DanielRosenwasser
Copy link
Member

let x: number[] = Array.from(
    [true, true, false]
)

Today, this code type checks because Array.from<T, U> takes an optional mapfn of type (el: T, index: number) => U.

When U is not inferred from the mapfn, it is instead inferred from the contextual type.

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Dec 13, 2017

Looks like this is a potential duplicate of #19682 (and was fixed in the signature of Array.from ), but isn't this indicative of a broader problem?

@sandersn
Copy link
Member

Yes:

declare function f<T, U = T>(xs: T[], g?: (x: T) => U): U[];
const ns: number[] = f([true]) //should error here: 'boolean' is not assignable to 'number'

The rules for defaults may specify that inference works this way currently, but I believe we decided change those rules in a recent design meeting.

@ajafff
Copy link
Contributor

ajafff commented Dec 13, 2017

This was reported a few times as a bug in one of TSLint's rules lately. Ref: palantir/tslint#3540 and duplicates

Would be awesome if this get's fixed.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jan 7, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.8 milestone Jan 7, 2018
@DanielRosenwasser DanielRosenwasser added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Jan 8, 2018
@DanielRosenwasser DanielRosenwasser removed this from the TypeScript 2.8 milestone Jan 8, 2018
@DanielRosenwasser
Copy link
Member Author

Spoke to @RyanCavanaugh, seems reasonable to say that this should really be two overloads, but it is still pretty undesirable.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants