Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection Types can be cast to any type V as long as it is nullable #45640

Closed
agrattaqrypt opened this issue Aug 30, 2021 · 6 comments
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@agrattaqrypt
Copy link

Bug Report

🔎 Search Terms

intersection
type
cast
null

🕗 Version & Regression Information

  • This is a false-negative type checking issue. An error should be generated, but is not.
  • This changed between versions 4.2.3 and 4.3.5 and 4.4.2
  • The incorrect behavior occurs in 4.3.5. The correct behavior occurs in 4.2.3 and 4.4.2

⏯ Playground Link

Playground link with relevant code

💻 Code

interface A {
    a: string;
}

interface B {
    b: string;
}

const objA: A = { a: "hello" }
const objB: B = { b: "hello" }
const objAB: A & B = { a: "hello", b: "world" }

const castA = objA as null; // always an error
const castB = objB as null; // always an error
const castAB = objAB as null; // error on TypeScript 4.2.3 and 4.4.2, not an error on TypeScript 4.3.5 
const castAB_string = objAB as string | null; // error on TypeScript 4.2.3 and 4.4.2, not an error on TypeScript 4.3.5 
const castAB_number = objAB as number | null; // error on TypeScript 4.2.3 and 4.4.2, not an error on TypeScript 4.3.5 

🙁 Actual behavior

In v4.3.5, we don't see the expected type error when casting an intersection type to null or null unions.
This issue does not occur in earlier or later versions.

🙂 Expected behavior

The type error behavior does not change depending on what version is being used, unless there is a well-documented breaking change.

@MartinJohns
Copy link
Contributor

Sounds like it was just a bug in 4.3.5.

@agrattaqrypt
Copy link
Author

Yes, I would assume this is something that needs to be patched on the 4.3.x minor version only

@agrattaqrypt
Copy link
Author

It's worth noting that 4.3.5 is the default version used in the latest VS Code, so it is probably widely used

@MartinJohns
Copy link
Contributor

It's worth noting that 4.3.5 is the default version used in the latest VS Code, so it is probably widely used

But you don't compile with the TypeScript version shipped with VSCode. And I'm confident that with the next August release of VSCode they will ship 4.4, as per microsoft/vscode#130601.

@agrattaqrypt
Copy link
Author

It's worth noting that 4.3.5 is the default version used in the latest VS Code, so it is probably widely used

But you don't compile with the TypeScript version shipped with VSCode. And I'm confident that with the next August release of VSCode they will ship 4.4, as per microsoft/vscode#130601.

Okay, well regardless of VS Code, there are people who use 4.3.x to build their code. 4.4.x has breaking changes. There should be a way to address this issue without requiring users to take on breaking changes. This is what the patch version is for.

Ultimately I can't make you fix it. I found a serious bug that wasted my time and I wanted to save other people the hassle by writing up a report.

@andrewbranch andrewbranch added Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Aug 30, 2021
@andrewbranch
Copy link
Member

We'll monitor comments on this issue, but if it wasn't noticed until a full version later, it's unlikely this warrants a patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants