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

Implementing Type which resolves to generic emits an error, but still infers correctly #22054

Closed
MadaraUchiha opened this issue Feb 20, 2018 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@MadaraUchiha
Copy link

MadaraUchiha commented Feb 20, 2018

TypeScript Version: 2.7.2

Search Terms:
typescript extend generic type
A class may only implement another class or interface.

Code

const actions = {
    foo(n: number) { },
    bar(s: string) { },
}

type Adaptor<T> = { [K in keyof T]: T[K] };

abstract class MobXAdaptor<T extends {}> implements Adaptor<T> {
    constructor(protected actions: T) { }
}

class MyMobxAdaptor2 extends MobXAdaptor<typeof actions> {
    foo(n: string) {
        return this.actions.foo(n);
    }

    bar() { }
}

Expected behavior:
One of:

  • Emit an error for Adaptor<T> not being an interface, and don't understand that foo() is not implemented correctly
  • Implement Adaptor<T> correctly, and emit an error for foo() not being implemented correctly.

Actual behavior:
Error is emitted for both Adaptor<T> not being an interface, and foo() not being implemented correctly.

This implies that even though TypeScript emits an error, it does manage to use the generic correctly.

Playground Link:
Click me

Related Issues:


Funny thing is, if I don't use <T> and instead use a realized type like {foo: string}, it doesn't complain about "A class may only implement another class or interface.", and still infers the type correctly in the extending class.

There's an inconsistency here, and I'm not sure which of the conflicting errors is the semantically correct one.

@ghost ghost added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 20, 2018
@ghost
Copy link

ghost commented Feb 20, 2018

I think this is working as intended. You can't implement a type parameter, so foo is just seen as a new method, not as an implementation of an interface. You can, however, have a property that is a type parameter, so we will still correctly give an error when using the actions property incorrectly. We're giving you the error on the call to actions.foo, not the foo in MyMobxAdaptor2 -- and as far as the compiler is concerned, these are two completely unrelated methods that happen to share a name.

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

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants