Closed
Description
Bug Report
π Search Terms
setter, union, wrong type
π Version & Regression Information
4.7.4
Also checked 3.9.7 with same results.
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about setter___
β― Playground Link
π» Code
class Nullable {
public set foo(_newValue : number | null) {
}
}
class NotNullable {
public set foo(_newValue : number) {
}
}
const nullable = new Nullable();
const notNullable = new NotNullable();
// correctly, we cannot assign "null" to "notNullable"
nullable.foo = null;
notNullable.foo = null; // This is correctly a compile error.
// When using a union of both types I would assume that you still cannot assign "null" because
// the union has to fullfill the constrains of both types. But, here you can assign "null".
function serviceMethod(a : Nullable | NotNullable) : void {
a.foo = null; // This is not a compile error, but I think it should be.
}
π Actual behavior
When using a union of two types with a setter where one is nullable and another one is not, the resulting setter is also nullable.
π Expected behavior
I believe this is wrong, as the union must ensure that the constraints of both types are fulfilled. Which mean that null
should be prohibited as one of the types does not allow it.