Skip to content

Suggestion: automatically infer class method param type #21223

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions