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

Allow inference of class generics when declaring an interface #32044

Open
5 tasks done
zakhenry opened this issue Jun 23, 2019 · 1 comment
Open
5 tasks done

Allow inference of class generics when declaring an interface #32044

zakhenry opened this issue Jun 23, 2019 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@zakhenry
Copy link

Search Terms

interface, inference, extending class

Suggestion

I'd like to be able to strongly type an interface such that when it is used on a class that has a generic I can infer the type of that generic to then use in the declaration of the interface.

Use Cases

I want to use this within a library that exposes both an abstract class and an optional interface that developers can opt-in to use. The abstract class has one generic parameter, and in order to correctly type the interface without duplicate generic declaration I want to be able to infer the abstract class' generic type.

Currently the inheriting class declarations must duplicate their generic type declaration.

This issue is similar to #26242 I think, but I don't see how the resolution of that issue will fix this one.

Examples

abstract class Base<T> {
    // implementation omitted for brevity
}

// ⬇️this is the proposed new syntax ⬇️
interface Remap<T> extends Base<infer U> {
    remap(input: U): T;
}

class Foo extends Base<string> implements Remap<number> {

    public remap(input: string): number {
        return input;
    }

}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jun 26, 2019
@pocesar
Copy link

pocesar commented Aug 2, 2019

oh I just saw this issue, it's pretty similar with I just wrote there, the infer keyword at interface level instead of invocation level

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants