Skip to content

Support for optional abstract members in an abstract class body #22939

Closed
@Jessidhia

Description

@Jessidhia

TypeScript Version: 2.8.0-insiders.20180320 (version currently running in playground)

Search Terms:
abstract optional

Code

abstract class Foo {
    abstract foo(): void
    abstract bar?(): void
}

// Non-abstract class Bar missing abstract member bar
class Bar extends Foo {
    foo() { return }
}

Expected behavior:
It should be possible to omit the definition of bar in the Bar class.

If something constructs an instance of Bar, it should not be possible to call new Bar().bar(), either because it is considered undefined or because it still retains some "abstractedness".

The intent is to use this in type definitions where subclasses can optionally define methods that will be called by their superclass; similar to lifecycle events in React.Component.

Not making them abstract allows things such as calling super.componentDidMount() even though it does not exist in React.Component's prototype, or having a subclass without their own render() implementation because TypeScript thinks it is provided by React.Component.

Supporting optional abstract members could also be used to guard against the common error of not initializing state on classes that use this.state -- it does not actually exist until either the first setState call or until this.state is assigned, and initializing this.state by assignment is far more common than initializing it with setState.

Actual behavior:
It's not possible to omit it unless bar = undefined is specifically added to Bar

Playground Link: Playground Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions