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

Generic methods with type parameters in generic constraints cannot be overridden #25373

Open
jcalz opened this issue Jul 2, 2018 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@jcalz
Copy link
Contributor

jcalz commented Jul 2, 2018

TypeScript Version: 3.0.0-dev.20180630

Search Terms: generic method, subclass, override, type parameter constraint

Code

class A {
    m<T, U extends T>(t: T, u: U) { }
}

class B extends A {
    m<T, U extends T>(t: T, u: U) { } 
//  ﹋ error
}

Expected behavior:
No errors; the m method in the subclass has exactly the same signature as the method it is overriding.

Actual behavior:
The compiler warns on the method override B.m:

Type '<T, U extends T>(t: T, u: U) => void' is not assignable 
  to type '<T, U extends T>(t: T, u: U) => void'.
Two different types with this name exist, but they are unrelated.
Types of parameters 'u' and 'u' are incompatible.
Type 'U' is not assignable to type 'T'.
Type 'T' is not assignable to type 'T'. 
Two different types with this name exist, but they are unrelated.

Note that even if you change the names of the type parameters in method B.m, as in:

class B extends A {
  m<T2, U2 extends T2>(t: T2, u: U2) { }
}

you still get the error Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.

If you remove the extends T generic constraint, it compiles.

Playground Link:
here

Related Issues:
Nothing obviously related; #23960 maybe?


So, is this a compiler bug? Or is it intended, and if so, what's the reasoning for it? This issue is inspired by a Stack Overflow question that left me scratching my head.

@RyanCavanaugh
Copy link
Member

Worked in 2.4, error introduced in 2.5

@npenin
Copy link

npenin commented Jan 10, 2020

any update on this one ? it breaks my CD since more than 1 year because of this bug.

@npenin
Copy link

npenin commented Apr 25, 2020

@RyanCavanaugh Any plan to fix this issue ? There might be a workaround using ts-ignore feature, but the ts-ignore comment is not ported over declaration files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants