Skip to content

Suggestion: automatically infer class method param type #21223

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

Closed
tommytroylin opened this issue Jan 17, 2018 · 4 comments
Closed

Suggestion: automatically infer class method param type #21223

tommytroylin opened this issue Jan 17, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@tommytroylin
Copy link

consider this code snippet

interface SomeClassType {
   name: string;
}

interface Plugin {
    apply(target: SomeClassType): void;
}

class SomePlugin implements Plugin {
    apply(target) { // [ts] Parameter 'target' implicitly has an 'any' type.
    }
}

class SomeOtherPlugin implements Plugin { // [ts] Class 'SomeOtherPlugin' incorrectly implements interface 'Plugin'.
    apply(target: string) {
    }
}

so the compiler knows exactly what type should param target to be.
but it can't be inferred.

add a simple type to that param should be ok.
but in some cases that SomeClassType is not exported directly at all.

can ts compiler infer that type from interface restrictions automatically?

Use case:
I'm writing a system which supports loading plugins.
I would like to export a single Plugin type to help plugin coders.

But it feels bad that

  • I must export all my basic types used in params
  • plugin coders should manually explicitly type them one by one in the correct order
@MarvinHannott
Copy link

Maybe it isn't all that great if everything is just inferred. Especially in cases like that it could lead to errors because the inferred type might not be what you think it should be. Maybe you mistakenly implemented the wrong interface, maybe you made a mistake in the ancestor interface and wouldn't notice at the time of implementing. Moreover, types serve self-documentation. If I need to know what SomePlugin looks like, I don't want to jump to Plugin first.
These two points are actually my biggest issues with Flow-Type from Facebook.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2018

Duplicate of #1373. more discussion in #10610 and in #3667

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 17, 2018
@tommytroylin
Copy link
Author

tommytroylin commented Jan 17, 2018

@MarvinHannott Not everything.
since we have interfaces that restrict class implementations, in most cases it should be good.
I agree with your point of view that this kind of inference could lead to errors. But on the other hand, duplicated type declarations are annoying. It goes worse when you have to import / export all of them.

maybe a inherit type may solve it elegantly

@mhegazy Thank you for the links. I 'll check them.
I've searched some issues but find nothing ...
maybe because I miss contextual keyword...

@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2018

nah.. these ones are hard to find.. I know them cause i look at them often.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants