Repository navigation
Allow abstract class for local declarations and expressions #9110
Description
Activity
aluanhaddad commented
on Jun 13, 2016 ContributorMore actionsYes it should but it doesn't make a lot of sense to do.
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".
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.Reacted by GoToLoop- changed the title
[-]"Modifiers cannot appear here" for abstract class in function[/-][+]Allow abstract class for local declarations and expressions[/+]on Jun 13, 2016 - addedSuggestionAn idea for TypeScriptAn idea for TypeScriptHelp WantedYou can do thisYou can do thisGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green light
on Jun 13, 2016 aluanhaddad commented
on Jun 13, 2016 ContributorMore actionsI 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...
Reacted by ulrichb and GoToLoopThis 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.)- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Aug 17, 2016 - locked and limited conversation to collaborators
on Jun 19, 2018
TypeScript Version:
1.8.9 / nightly (1.9.0-dev.20160612-1.0)
Code
Expected behavior:
It should be possible to define abstract classes within functions just like non-abstract ones.