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

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

Open
JeanMeche opened this issue Nov 25, 2024 · 5 comments
Open

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

JeanMeche opened this issue Nov 25, 2024 · 5 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@JeanMeche
Copy link
Contributor

πŸ”Ž 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

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Nov 27, 2024
@jcalz
Copy link
Contributor

jcalz commented Nov 28, 2024

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.

@Andarist
Copy link
Contributor

I agree with @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.

@JeanMeche
Copy link
Contributor Author

JeanMeche commented Nov 28, 2024

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

@Andarist
Copy link
Contributor

Andarist commented Nov 28, 2024

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;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants