Skip to content

Union of a nullable and not nullable setter results wrongly into a nullable setterΒ #50142

Closed
@MiladSadinam

Description

@MiladSadinam

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.

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