Skip to content

Abstract Properties #4669

Description

This is a proposal to extend #3578 to allow abstract properties in abstract classes. So the following should be legal:

abstract class A {
  abstract p;
}
class B extends A {
  get p() {...}
  set p(v) {...}
}

Activity

  1. added
    SuggestionAn idea for TypeScript
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    and removed on Sep 8, 2015
  2. Pajn commented on Sep 27, 2015

    @Pajn

    +1

    Also abstract getters should be full-fillable by properties:

    abstract class A {
      abstract get p();
    }
    class B extends A {
      p;
    }

    This is a pretty common pattern in Dart when the abstract class just need some values by it's implementers.

  3. added
    DeclinedThe issue was declined as something which matches the TypeScript vision
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Oct 2, 2015
  4. mhegazy commented on Oct 2, 2015

    @mhegazy
    Contributor

    declaring the property as abstract does not change anything different from just declaring it. the type is going to always have the property.

    As for getters and setters., the type system does not differentiate between getter/setter and a property declaration.

  5. denvned commented on Oct 4, 2015

    @denvned
    ContributorAuthor

    Mohamed Hegazy (@mhegazy) Actually, there is an important difference. Using an abstract property I could specify that the property must be specialized (usually with getters and/or setters) in a derived class, and the compiler should rise an error if it's not specialized.

    Please reconsider closing this proposal.

  6. raveclassic commented on Oct 8, 2015

    @raveclassic

    Mohamed Hegazy (@mhegazy) I totally agree with Denis Nedelyaev (@denvned) about enforcing getter declaration in child classes.

  7. bykbtzr commented on Oct 20, 2015

    @bykbtzr

    The lack of abstract properties allows child classes to simply ignore some important properties in my contracts.

    Please reopen this for further discussion...

  8. craigbroadman commented on Oct 29, 2015

    @craigbroadman

    +1

    Especially as lambda/arrow functions are actually properties as I found out when I was trying to implement the following functionality but it wouldn't compile due to....

    "Class 'Base' defines instance member function 'def', but extended class 'Concrete' defines it as instance member property"...

    abstract class Base {
        abstract abc(): void;
        abstract def(): void;
    }
    
    class Concrete extends Base {
        private setting: boolean;
    
        public abc(): void  {
            this.setting = true;
        }
    
        public def = (): void => {
            this.setting = false;
        }
    }
    
  9. raveclassic commented on Oct 29, 2015

    @raveclassic

    Please reopen for discussion!

    Here is a simple example describing the problem:

    abstract class Foo {
        abstract getName(); //works like a charm
        abstract setName(value);
    
        /* but it's better to use properties:
        abstract get name();
        abstract set name(value);
        */
    
        /* I can define properties */
        private _name = '';
        get name() {
            return this._name;
        }
        set name(value) {
            this.name = value; 
        }
        /* but cannot force Bar class to implement them */
    }
    
    class Bar extends Foo {
        //complains only about getName and setName
    }

    Compiler complains about undefined getName and setName but I can't define property contract.
    But hey, JS and TS both support getters/setters - why should I use Java-style methods instead of properties?

  10. 26 remaining items

  11. RyanCavanaugh commented on Feb 1, 2016

    @RyanCavanaugh
    Member

    Mohamed Hegazy (@mhegazy) hand this one off?

  12. mhegazy commented on Feb 1, 2016

    @mhegazy
    Contributor

    PRs are welcomed also.

  13. mhegazy commented on Feb 1, 2016

    @mhegazy
    Contributor

    Note, the conclusion form the design discussion was abstract readonly p: number was the desired order of modifiers.

  14. sandersn commented on Feb 22, 2016

    @sandersn
    Member

    #7184 allows for abstract properties (and accessors). There's not much to the change because the machinery is the same as for abstract methods.

  15. tehsenaus commented on Mar 4, 2016

    @tehsenaus

    Awesome 👍

    Which version is this landing in?

  16. sandersn commented on Mar 4, 2016

    @sandersn
    Member

    It should already be in typescript@next. It will ship in 2.0.

  17. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions