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
https://www.typescriptlang.org/play?#code/MYGwhgzhAEByCuJwCMQFNoG8BQBIADvKgJbDQRoAu0AZgPZ0AUA+gHZoDuAamCPBgC5oreAFtkaAE7QAPsMQgAlNCx4Avtg3ZQkGLDqUESMKgw4CREKXJVaDFu269+0ISPFTlq3Bq3A6rBDUIsam0AC8wpxwCibojIoA3NoBQcIGRijoEVEccBmxpgnJ2AD0pdD+kpJowJQgAJ4ANNAcGMBgrKwG0LrEAOas0ABEISDD0JR0I92GhejD2GNxaAB09NORY8mzmSvrDDnb0OXQACoAFsQw15V01bX1Db13ovjE2VKS96vYZRUAdQuaCG8AgxFY-Re8FYxAC0DoNGgyAMF0mDXwaBgAElWnREAATXpQMQYSgXMDUBr48iUD4gSqdWbE8GDGYKCYSDpgtD-SbA6AwuFDCkwKa0BQ0en89qpSiSMAQmCI5Go9GYiCraAAIXglBawJq0Gp8EZQz6bNGHN+NBhdWFNkkADdSGgALJUC50AmMMCuGKhbJyfRzQNoZRCJ10YhE8xgA6beRIRInCqXW63Zl+-xvD4YL73FrIPXQXHkiEAa2gxGoEC9hORa00QA
π» 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.
Bug Report
π Search Terms
setter, union, wrong type
π Version & Regression Information
4.7.4
Also checked 3.9.7 with same results.
β― Playground Link
https://www.typescriptlang.org/play?#code/MYGwhgzhAEByCuJwCMQFNoG8BQBIADvKgJbDQRoAu0AZgPZ0AUA+gHZoDuAamCPBgC5oreAFtkaAE7QAPsMQgAlNCx4Avtg3ZQkGLDqUESMKgw4CREKXJVaDFu269+0ISPFTlq3Bq3A6rBDUIsam0AC8wpxwCibojIoA3NoBQcIGRijoEVEccBmxpgnJ2AD0pdD+kpJowJQgAJ4ANNAcGMBgrKwG0LrEAOas0ABEISDD0JR0I92GhejD2GNxaAB09NORY8mzmSvrDDnb0OXQACoAFsQw15V01bX1Db13ovjE2VKS96vYZRUAdQuaCG8AgxFY-Re8FYxAC0DoNGgyAMF0mDXwaBgAElWnREAATXpQMQYSgXMDUBr48iUD4gSqdWbE8GDGYKCYSDpgtD-SbA6AwuFDCkwKa0BQ0en89qpSiSMAQmCI5Go9GYiCraAAIXglBawJq0Gp8EZQz6bNGHN+NBhdWFNkkADdSGgALJUC50AmMMCuGKhbJyfRzQNoZRCJ10YhE8xgA6beRIRInCqXW63Zl+-xvD4YL73FrIPXQXHkiEAa2gxGoEC9hORa00QA
π» Code
π 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
nullshould be prohibited as one of the types does not allow it.