A readonly class property that overrides a non-readonly property can still be reassigned in the base class (and vice-versa) · Issue #8496 · microsoft/TypeScript (original) (raw)

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.