Skip to content

A readonly class property that overrides a non-readonly property can still be reassigned in the base class (and vice-versa) #8496

Closed
@malibuzios

Description

@malibuzios

TypeScript Version:
1.9.0-dev.20160506

Code

class Base {
    prop: number;

    baseMethod() {
        this.prop = 4321;
    }
}

class Derived extends Base {
    readonly prop: number = 1234; // this could also be an accidental name collision

    derivedMethod() {
        this.baseMethod();
    }
}

let x = new Derived();
console.log(x.prop); // prints 1234
x.derivedMethod();
console.log(x.prop); // prints 4321

Expected behavior:
Trying to re-declare prop as readonly in Derived should error:

A writable property cannot be overriden by a readonly property

Actual behavior:
No error, prop can be reassigned in the base class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions