Skip to content

Strict property initialization (arguably) confused by redefinition of property in subclass #21775

Closed
@ethanresnick

Description

@ethanresnick

TypeScript Version: 2.7.1

Search Terms:
property initialized used sub class

Code

class Super {
    protected prop: { a: number; };
    constructor() {
        this.prop = { a: 1 };
    }
}

class Sub extends Super {
    protected prop: { a: number, b: number };

    constructor() {
        super();
        this.prop = {
            ...this.prop,
            b: 2
        };
    }
}

Expected behavior:
The assignment to this.prop in the subclasss' constructor should succeed, with the compiler understanding that this.prop, at that stage, has the value + type given to it by the superclass.

Actual behavior:
Error: Property 'prop' is used before being assigned.

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