Skip to content

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

Closed

Description

@agrattaqrypt

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.

Activity

  1. MartinJohns commented on Aug 30, 2021

    @MartinJohns
    Contributor

    Sounds like it was just a bug in 4.3.5.

  2. agrattaqrypt commented on Aug 30, 2021

    @agrattaqrypt
    Author

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

  3. agrattaqrypt commented on Aug 30, 2021

    @agrattaqrypt
    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

  4. MartinJohns commented on Aug 30, 2021

    @MartinJohns
    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.

  5. agrattaqrypt commented on Aug 30, 2021

    @agrattaqrypt
    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.

  6. andrewbranch commented on Aug 30, 2021

    @andrewbranch
    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.

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions