Skip to content

Static inheritance behavior is surprising #2082

Closed
@jvilk

Description

@jvilk

I recently learned that TypeScript classes inherit static methods and fields, which really surprised me! Unlike object instances, static properties aren't set in a constructor, so their static initialization can't be re-run for subclasses. As a result, inherited static fields in subclasses will take on the current value of their parents' field as their initial value, which can be surprising.

class A {
    static bar: string = "foo";
}

A.bar = "Gotcha";

class B extends A {
}

console.log(B.bar); // prints 'Gotcha'

Is this desired behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions