Skip to content

Abstract class type argument inference #13087

Open

Description

@tinganho

It would be good if TypeScript can infer the type of an extended class of an generic abstract class:

abstract class A<T> {
    abstract a: T;
    get(): T {
        return '' as any;
    }
}

class B extends A {
    a: 'helloworld';
}

const b = new B():
b.get(); // string

In a similar fashion, it does inference elsewhere.

Activity

  1. aluanhaddad commented on Dec 21, 2016

    @aluanhaddad
    Contributor

    This is a really interesting idea. Seems like it would improve life for React users for sure.
    Is there any particular reason it would be limited to abstract classes? It seems applicable to interfaces as well as concrete classes.

  2. tinganho commented on Dec 22, 2016

    @tinganho
    ContributorAuthor

    Aluan Haddad (@aluanhaddad) I suggested it applied to abstract, mostly because abstract implies there will be at least another declaration. But after thinking about it, I cannot see why it's not applicable to normal classes as well.

  3. RyanCavanaugh commented on Jan 10, 2017

    @RyanCavanaugh
    Member

    See notes at #13368

  4. tinganho commented on Jan 10, 2017

    @tinganho
    ContributorAuthor

    I hope this covers statics as well, example taken from #10603:

    abstract class BaseClass<T> {
        abstract static defaultValue: T;
        // ...
    }
    
    class ClassA extends BaseClass {
        static defaultValue = {
             name: 1;
        }
    }
  5. infeng commented on Jan 11, 2017

    @infeng

    I hope it apply to normal class also. see ant-design/ant-design#4324

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

    CommittedThe team has roadmapped this issueDomain: JavaScriptThe issue relates to JavaScript specificallySuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions