You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportinterfaceIMiddleware<TInput,TOutput>{run(input: TInput): TOutput;}typeInput={x: number};typeOutput={y: number};classMiddlewareImplimplementsIMiddleware<Input,Output>{run(input){// input should be infered as Input instead of any (since Input is a supertype for input param)returninput// error should be displayed - {x: number} is not assignable to {y: number} }}constclassMiddleware=newMiddlewareImpl();// classMiddleware is actually IMiddleware<any, any> instead of IMiddleware<Input, Output> even if it's clearly annotated that MiddlewareImpl implements (at least) IMiddleware<Input, Output>classMiddleware.run({});// error should be thrown: Property 'x' is missing in type '{}' but required in type 'Input'
Expected behavior:
The generic type of the implemented interface shouldn't be replaced with any, if the implemented method doesn't explicitly widens it to any
Actual behavior:
If a class implementing a given interface doesn't explicitly define types for a method parameter that uses generic types passed to the interface, then the parameter is cast to any, making the final type wider than expected.
I wasn't sure if this shouldn't be reported rather as a "feature request", but I finally chose bug, since I found this behavior highly unintuitive for somebody who does not use noImplicitAny
TypeScript Version: 3.9.2
Search Terms:
interface generic any, interface method inference
Code
Expected behavior:
The generic type of the implemented interface shouldn't be replaced with
any
, if the implemented method doesn't explicitly widens it toany
Actual behavior:
If a class implementing a given interface doesn't explicitly define types for a method parameter that uses generic types passed to the interface, then the parameter is cast to
any,
making the final type wider than expected.I wasn't sure if this shouldn't be reported rather as a "feature request", but I finally chose bug, since I found this behavior highly unintuitive for somebody who does not use
noImplicitAny
Playground Link: https://www.typescriptlang.org/play/?noImplicitAny=false#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgSQFkEATEgG2AHdMpgAeAFXyTAFcYAaORgeQ-YwAfHADeAKDhwobJAApkggFw8WggJQq+AjgG5xAX3HiYATzB41HOAF4xcECqRsAtgCM0cA-rMW4-GEFbe1MnVw8oL31xbHJMAGd4uGIySho6fBcwckQsyhdgFCSiUgpqWgYrLn8dYTFJaVkFVg51eykOgHpOxBb4eIALCDZyEjgPXvQ0YDGEgj7e+NRMMYh0OEwkUzg5eORceaCEJMw4eLYLWHM8dGheoLBaTBd1Bqk6GDYoJHvrLp60FA7oNhqNxngSMdsphTDM4ABaMSOODOdxoAyIJJICDwBJ7ADmSEwbkocBgEDEoRR4XRcAaRiMMQgSCWcFieJSZXSeDsSGoyVKaQqmWycnUujg3TZcUSnKFdExG2wn0w5HI2xKqXKdHom1M3D1ImQS2AKzgawIcu1lT63ACghEwAAboVEOsEDAAORJWKmqDqjZIbEwTCoMYwAahgVa7kinIIPLAApFHZRygJGBtTVcir0Kp22pCGIy+JW7kAOhk8lEBnFHXrDcb9algOBQxGYwmEaBVCQKgACkDLmY4J6QJ7FS5jnskPjemTrqOaxO3NY6ABHNgIOhjZALvyeqoT+tAA
Related Issues: -
The text was updated successfully, but these errors were encountered: