Skip to content

Bad type inference of function overloads #20396

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

Closed
gcnew opened this issue Dec 1, 2017 · 5 comments
Closed

Bad type inference of function overloads #20396

gcnew opened this issue Dec 1, 2017 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@gcnew
Copy link
Contributor

gcnew commented Dec 1, 2017

TypeScript Version: 2.7.0-dev.20171110

Code

interface Leaf<T> {
    kind: 'leaf',
    data: T[]
}

interface Branch<T> {
    kind: 'branch',
    data: Trie<T>[]
}

type Trie<T> = Leaf<T> | Branch<T>

function mapTrie<T, U>(
    trie: Trie<T>,
    f: {
        (x: T): U,
        (x: Trie<T>): Trie<U>
    }
): Trie<U> {
    if (trie.kind === 'leaf') {
        return {
            kind: 'leaf',
            data: trie.data.map(f) // `f` should be inferred as `(x: T) => U`
                                   // works fine with explicit type param `map<U>(f)`
        };
    }

    return {
        kind: 'branch',
        data: trie.data.map(f)
    };
}
@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2017

Please see #20257

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 2, 2017
@gcnew
Copy link
Contributor Author

gcnew commented Dec 8, 2017

I don't think this issue a duplicate of #20257. While the underlying limitation might be the same, in #20257 there is no definite clue which overload is the expected one. In this issue, however, proper inference can be made following the types of the parameters.

What we know:

trie.data: T[]
expected mapper parameter: (x: T) => <some B>
provided mapper argument: ((x: T) => U) & ((x: Trie<T>) => Trie<U>)

We are looking for a B which can be deduced from f's signature. The following two options are present:

  • (x: T) => <some B> ~ (x: T) => U)
  • (x: T) => <some B> ~ (x: Trie<T>) => Trie<U>

The first option yields the correct solution B = U. The second one is not solvable, as T !== Trie<T>, so it should be discarded. What actually happens is that the second option creeps in, although it does not satisfy the first parameter's type x: T.

@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.

@gcnew
Copy link
Contributor Author

gcnew commented Jan 2, 2018

@mhegazy Does my last comment change your view on this issue?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 11, 2018

do not think so..

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants