镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Casting U|T to U is newly an error in nightly (4.8.0-dev.20220609) #49457

Description

@frigus02

Bug Report

🔎 Search Terms

TS2352, sufficiently overlaps

🕗 Version & Regression Information

  • This changed between versions 4.7.2 and v4.8.0-dev.20220609

⏯ Playground Link

Playground link (4.7.2) with relevant code (no error)

Playground link (nightly) with relevant code showing the error

💻 Code

function f<U, T, X extends U|T>(x: X) {
    const _: U = x as U;
//               ^^^^^^
// Conversion of type 'X' to type 'U' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   'U' could be instantiated with an arbitrary type which could be unrelated to 'X'.(2352)
}

🙁 Actual behavior

This is an error in nightly (4.8.0) but was fine in 4.7.2.

🙂 Expected behavior

The new error doesn't feel helpful to me. I expect assigning U|T to U to be an error. But the explicit cast feels enough to me because U|T seem related to U. I don't feel like casting through unknown should be necessary.

Activity

  1. fatcerberus commented on Jun 9, 2022

    @fatcerberus

    U | T is related to U, but X might not be. For example if U and T are completely disjoint types and X is T, casting it to U would almost certainly be a mistake. In fact, that's the exact possibility the error message is hinting at.

  2. RyanCavanaugh commented on Jun 9, 2022

    @RyanCavanaugh
    Member

    This wasn't an intentional change AFAIK

  3. fatcerberus commented on Jun 9, 2022

    @fatcerberus

    I mean, the error message is technically true though. 🤷‍♂️

  4. ahejlsberg commented on Jun 9, 2022

    @ahejlsberg
    Member

    TypeScript Bot (@typescript-bot) bisect good v4.7.2 bad main

  5. ahejlsberg commented on Jun 9, 2022

    @ahejlsberg
    Member

    We'll see what the bisect says, but I'm pretty sure this is caused by #48861.

    Wesley Wigham (@weswigham) Thoughts?

  6. ahejlsberg commented on Jun 9, 2022

    @ahejlsberg
    Member

    It is indeed caused by #48861. The code in that PR disallows relating type parameters unless one extends the other in a direct chain. In other words, it doesn't make provisions for union type constraints.

  7. weswigham commented on Jun 9, 2022

    @weswigham
    Member

    Yup. We could expand it, but as noted by the poster above, it's definitely not a particularly safe cast; it's neither a downcast nor an upcast.

  8. frigus02 commented on Jun 10, 2022

    @frigus02
    ContributorAuthor

    My argument is that U|T is enough of a relation to U that an explicit cast is enough.

    it's definitely not a particularly safe cast

    That is true, though. I could understand a decision either way.

  9. fatcerberus commented on Jun 10, 2022

    @fatcerberus

    My argument is that U|T is enough of a relation to U that an explicit cast is enough.

    See my post above. imo, whether U | T is related to U is a red herring, because X is generic. The better question is, is the type bound to X always related to the one bound to U, and the answer is "not necessarily".

  10. ahejlsberg commented on Dec 8, 2022

    @ahejlsberg
    Member

    I'm going to close this as working as intended.

  11. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    BugA bug in TypeScript
    on Dec 8, 2022
  12. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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