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 type for an interface method is infered as any #38810

Closed
robak86 opened this issue May 27, 2020 · 2 comments
Closed

Generic type for an interface method is infered as any #38810

robak86 opened this issue May 27, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@robak86
Copy link

robak86 commented May 27, 2020

TypeScript Version: 3.9.2

Search Terms:
interface generic any, interface method inference

Code

export interface IMiddleware<TInput, TOutput> {
  run(input: TInput): TOutput;
}

type Input = { x: number };
type Output = { y: number };

class MiddlewareImpl implements IMiddleware<Input, Output> {
  run(input) {      // input should be infered as Input instead of any (since Input is a supertype for input param)
    return input    // error should be displayed - {x: number} is not assignable to {y: number} 
  }
}

const classMiddleware = new MiddlewareImpl(); // 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

Playground Link: https://www.typescriptlang.org/play/?noImplicitAny=false#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgSQFkEATEgG2AHdMpgAeAFXyTAFcYAaORgeQ-YwAfHADeAKDhwobJAApkggFw8WggJQq+AjgG5xAX3HiYATzB41HOAF4xcECqRsAtgCM0cA-rMW4-GEFbe1MnVw8oL31xbHJMAGd4uGIySho6fBcwckQsyhdgFCSiUgpqWgYrLn8dYTFJaVkFVg51eykOgHpOxBb4eIALCDZyEjgPXvQ0YDGEgj7e+NRMMYh0OEwkUzg5eORceaCEJMw4eLYLWHM8dGheoLBaTBd1Bqk6GDYoJHvrLp60FA7oNhqNxngSMdsphTDM4ABaMSOODOdxoAyIJJICDwBJ7ADmSEwbkocBgEDEoRR4XRcAaRiMMQgSCWcFieJSZXSeDsSGoyVKaQqmWycnUujg3TZcUSnKFdExG2wn0w5HI2xKqXKdHom1M3D1ImQS2AKzgawIcu1lT63ACghEwAAboVEOsEDAAORJWKmqDqjZIbEwTCoMYwAahgVa7kinIIPLAApFHZRygJGBtTVcir0Kp22pCGIy+JW7kAOhk8lEBnFHXrDcb9algOBQxGYwmEaBVCQKgACkDLmY4J6QJ7FS5jnskPjemTrqOaxO3NY6ABHNgIOhjZALvyeqoT+tAA

Related Issues: -

@DanielRosenwasser
Copy link
Member

Frustrating, but a duplicate of #23911

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label May 27, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants