Skip to content

NoInfer interfere with Exclude in Exclude<NoInfer<I>, undefined>` #60594

Closed as not planned

Description

🔎 Search Terms

  • NoInfer
  • inteference
  • Exclude

🕗 Version & Regression Information

This is not a regression, and doesn't work with 5.7 :

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAogHgYwDYFcAmECqA7DAzAS2wjQB4AlAPigF5ZFUNSA5AewEls8IAnCygDRQUuCIWJpKAbgBQMpBGBRsARgBc9ZOiyjxJUthQBbAEa8oAH2G6iJanRH5baKVDduA9B6isA1vMVlACYNeC0MHCcJFmMzHktrKLtaRLFnV3coL2VWJQgGbTQApQBndU1GHSSyEuAeIgBzBMc0iXtUvRdMrO8-YqgSkIrtSNb9AGU6xuabNpSWzoz3bOxcqHzwkn60crDK0c7SABEAQ2BoKwXndquJJcyVtY3KooUlE54eXYKI2f1DUy8ADaAF0ZtUbn8uo88j8tm8fCYAFbfTYHZykADeAF9wWNJPMofdlr1-AjDEgkKj9lCDChKXjOpDqvdsn0gA

💻 Code

type ExcludeUndefined<R> = Exclude<NoInfer<R>, undefined>;

let n1: ExcludeUndefined<number | undefined> = undefined;     // ok
let n2: ExcludeUndefined<Number | undefined> = undefined;     // not excluded
let s1: ExcludeUndefined<string | undefined> = undefined;     // ok
let s2: ExcludeUndefined<String | undefined> = undefined;     // not excluded
let d1: ExcludeUndefined<Date | undefined> = undefined;       // not excluded
let arr1: ExcludeUndefined<number[] | undefined> = undefined; // not excluded
let obj1: ExcludeUndefined<{} | undefined> = undefined;       // ok
let null1: ExcludeUndefined<null | undefined> = undefined;    // ok

🙁 Actual behavior

undefined is not consistantly excluded.

🙂 Expected behavior

undefined should be consistantly excluded.

Additional information about the issue

No response

Activity

  1. RyanCavanaugh commented on Nov 27, 2024

    @RyanCavanaugh
    Member

    This isn't a supported way to use NoInfer. Not sure what the intent of this is.

  2. added
    Not a Defect
    on Nov 27, 2024
  3. jcalz commented on Nov 28, 2024

    @jcalz
    Contributor

    What if you have a function whose return type involves NoInfer because you don't want it to be inferred from the variable to which you assign it? I mean, if you have a function that looks like declare function foo<T>(⋯): Exclude<NoInfer<T>, undefined> then you could end up with foo<Date | undefined>(⋯) producing one of these things.

  4. Andarist commented on Nov 28, 2024

    @Andarist
    Contributor

    I agree with Joe Calzaretta (@jcalz) and NoInfering within a return type is actually something I recommended last month to somebody as a fix for their problems. It's a legit pattern.

  5. JeanMeche commented on Nov 28, 2024

    @JeanMeche
    ContributorAuthor

    I noticed that NoInfer<Exclude<R, undefined>> actually works, but does it break the NoInfer behavior ?

  6. Andarist commented on Nov 28, 2024

    @Andarist
    Contributor

    It shuldn't break it. Whatever is wrapped with NoInfer shouldn't be inferred into. It's an early exit in inferFromTypes:

    function inferFromTypes(source: Type, target: Type): void {
        if (!couldContainTypeVariables(target) || isNoInferType(target)) {
            return;
        }
  7. typescript-bot commented on Dec 1, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  8. 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

Assignees

No one assigned

    Labels

    Not a Defect

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions