Skip to content

Allow abstract class for local declarations and expressions #9110

Description

@ulrichb

TypeScript Version:

1.8.9 / nightly (1.9.0-dev.20160612-1.0)

Code

(() => {
    // works:
    class SomeClass { }

    // emits "error TS1184: Modifiers cannot appear here":
    abstract class SomeAbstractClass { }
})();

Expected behavior:
It should be possible to define abstract classes within functions just like non-abstract ones.

Activity

  1. aluanhaddad commented on Jun 13, 2016

    @aluanhaddad
    Contributor

    Yes it should but it doesn't make a lot of sense to do.

  2. yortus commented on Jun 13, 2016

    @yortus
    Contributor

    It might make sense according to the same rationale that TypeScript supports other local types. It would be helpful to see a motivating example showing the value of a local abstract class.

    Note this was sort-of anticipated in the proposal for abstract classes, under the heading "To Be Discussed".

  3. ulrichb commented on Jun 13, 2016

    @ulrichb
    Author

    I had no real use case, I just wanted to scope the abstract class and its derived types to avoid having global symbols. The workaround was to use a surrounding namespace. It was an artificial sample for a TypeScript talk.

    Nevertheless, as local types (for non-abstract classes, interfaces, and enums) are supported, IMO missing support for abstract classes means that this feature is incomplete. Also because the compiler output seems to be correct (without --noEmitOnError) except the mentioned error message.

  4. changed the title [-]"Modifiers cannot appear here" for abstract class in function[/-] [+]Allow abstract class for local declarations and expressions[/+] on Jun 13, 2016
  5. aluanhaddad commented on Jun 13, 2016

    @aluanhaddad
    Contributor

    I agree this should be supported I was just curious what the actual use case was since the base class cannot escape the function anyway, it's only implementations are going to reside within the same scope which should be fairly narrow. However if you're using the function to avoid polluting the global namespace but want to use an abstract class in your design that seems very reasonable. I've been spending too much time writing external modules...

  6. ghost closed this as completedin #9994on Jul 28, 2016
  7. rauschma commented on Aug 10, 2016

    @rauschma

    This is a use case for abstract class expressions: implementing mixins via subclass factories.

    const MyMixin = (S : { new () : Object }) => abstract class extends S {
        abstract foo() {}
        bar() {}
    };
    
    // Must implement foo()
    class MyClass extends MyMixin(Object) {
    }

    (I don’t feel 100% confident about the type of S; there must be a better way to type it.)

  8. added this to the milestone on Aug 17, 2016
  9. 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

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions