TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Type interference fails for generics in multiple levels

Open jeremyhallden opened this issue 10 years ago • 7 comments

TypeScript Version: 1.8.0

Considering the code below, the line in the foo method does not compile:


export class Dummy{
   public static foo<T extends  IEquatable<T>>(graphEdges: Array<GraphEdge<T>>) {
           const dummy = graphEdges.map(edge => edge.endNodeId);
           (...)
   }
}

export interface IEquatable<T> {
        equals(other: T): boolean;
    }

export class GraphEdge<T extends IEquatable<T>> implements IEquatable<GraphEdge<T>> {
        constructor(
            public startNodeId: T,
            public endNodeId: T) {
        }

        public equals(other: GraphEdge<T>): boolean {
            if (other == null) {
                return false;
            }
            return this.startNodeId.equals(other.startNodeId) && this.endNodeId.equals(other.endNodeId);
        }
    }

This should give me an array of type Array[T], but gives a compile error: "Supplied parameters do not match any signature of call target: parameter type mismatch. Parameter 'callbackfn' should have type assignable to '{(value: GraphEdge[T], index: number, array: GraphEdge[T][]):any}

jeremyhallden avatar Jul 08 '16 11:07 jeremyhallden

@jeremyhallden There is an error in your code.

const foo = new Array<GraphEdge<T>>().map(edge => edge.endNodeId);

You need to instantiate the generic parameter T. as in

const foo = new Array<GraphEdge<number>>().map(edge => edge.endNodeId); // number[]

aluanhaddad avatar Jul 08 '16 14:07 aluanhaddad

@aluanhaddad Thanks for the response, but I (over)simplified the code to show the error. I'll edit my initial post right now :)

jeremyhallden avatar Jul 11 '16 12:07 jeremyhallden

@jeremyhallden I see, I'll try to take a look at the revised code later

aluanhaddad avatar Jul 11 '16 13:07 aluanhaddad

@jeremyhallden I tried your example with my compiler built from master and I don't see an error (although I had to convert foo from public static to function). Can you try this code with typescript@next? That is, by saying npm install -gtypescript@next` and using that typescript compiler.

sandersn avatar Jul 11 '16 16:07 sandersn

Thanks for trying it out on 2.0 - I can try to do the same soon. This is to be used in a huge app that is in production right now, which runs 1.8 though. Could you confirm that this bug exists in 1.8.x?

I don't understand what you mean when you say that you couldn't use public static though? The code I've pasted is ofc not the actual code base - the static foo function is actually in a class..

On Mon, Jul 11, 2016 at 6:15 PM, Nathan Shively-Sanders < [email protected]> wrote:

@jeremyhallden https://github.com/jeremyhallden I tried your example with my compiler built from master and I don't see an error (although I had to convert foo from public static to function). Can you try this code with typescript@next? That is, by saying npm install -gtypescript@next` and using that typescript compiler.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Microsoft/TypeScript/issues/9571#issuecomment-231783940, or mute the thread https://github.com/notifications/unsubscribe/AGAf4UfRJWWFaqPjqDO8rCVunvLp7NLFks5qUmwjgaJpZM4JH8BP .

jeremyhallden avatar Jul 11 '16 16:07 jeremyhallden

I went back to 1.8.34 and also couldn't reproduce the error. Here's precisely what I have in my VSCode buffer:

function foo<T extends  IEquatable<T>>(graphEdges: Array<GraphEdge<T>>) {
        const dummy = graphEdges.map(edge => edge.endNodeId);
        //(...)
}

export interface IEquatable<T> {
        equals(other: T): boolean;
    }

export class GraphEdge<T extends IEquatable<T>> implements IEquatable<GraphEdge<T>> {
    constructor(
        public startNodeId: T,
        public endNodeId: T) {
    }

    public equals(other: GraphEdge<T>): boolean {
        if (other == null) {
            return false;
        }
        return this.startNodeId.equals(other.startNodeId) && this.endNodeId.equals(other.endNodeId);
    }
}

sandersn avatar Jul 11 '16 17:07 sandersn

Without more information we can be sure we identified the issue. please try typescript@next and reopen if it is still an issue. also please provide more information to be able to diagnose and track the issue.

mhegazy avatar Sep 21 '16 04:09 mhegazy