GeezerVison is showing github.com — Allow abstract class for local declarations and expressions · Issue #9110 · microsoft/TypeScript
Allow abstract class for local declarations and expressions #9110
Description
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
aluanhaddad commented on Jun 13, 2016
Yes it should but it doesn't make a lot of sense to do.
yortus commented on Jun 13, 2016
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".
ulrichb commented on Jun 13, 2016
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.
changed the title
[-]"Modifiers cannot appear here" for abstract class in function[/-] [+]Allow abstract class for local declarations and expressions[/+] on Jun 13, 2016aluanhaddad commented on Jun 13, 2016
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...
Reacted by ulrichb and GoToLoop
rauschma commented on Aug 10, 2016
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.)
added this to the
This milestone has been deleted milestone
on Aug 17, 2016
Metadata
Metadata
Development
No branches or pull requestsIssue actions