Skip to content

initialized class variables in base class constructor are null #15601

Closed
@venelin-mihaylov

Description

@venelin-mihaylov

TypeScript Version: 2.3.2
CODE

interface IMember {
      name?: string
    }

    interface IBase {
      member1?: IMember
    }

    class Base implements IBase {
      public get member1() {
        return this._member1
      }
      public _member1: IMember
      public constructor() {
        console.log('parent constructor this')
        console.log(this)
        console.log('parent constructor _member1')
        const a = this._member1;
        console.log(a)
        expect(a).to.not.be.undefined
      }
    }

    class Child extends Base {
      public _member1: IMember = {
        name: 'grrrr'
      }
    }

    const o = new Child()

Expected Behavior:
I initialize a member variable in the Child class during class declaration.
I expect in the parent class constructor the class variable to be initialized.

Actual behavior:
It is not.
However, it is initialized after the object is constructed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions