Microsoft/TypeScript

Binary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain

Open

#56.998 aperta il 9 gen 2024

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Experience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

🔎 Search Terms

binary condition refinement predicate optional chain undefined

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240109#code/C4TwDgpgBAKu0F4oG8BQUoEsAmAuKAdgK4C2ARhAE4DcqAvraqJFAJIEBmVEBAxhOw4B7KEjQZmEAAoBDSjJIRgVfHEi0MmEmAA2mCNgCClTKAD8+YuSq0GqVNgi8dc6ByJ9gmIQSgBzJUFuPgFOIQAxIUo1CAAKSVV4AEp8IMoefkERAB8oD0cOTAIDRndPb18OAgBGeOrEyAAaKGAAJgaIJJR0KB0lLDDRf0DOYMywyOj4OqSNLA4oWKLhMwA6LV19IxNQKABCBCR8iELi7C7xDAHhde09A2NTEDm6KAgdAGdoTAXYpcGkAFgGkMqFhJMYvFWkkkmsNvdtk99oc8gQCkUDBcepowrdNg8ds8enR6PYyrwvD4oFVWnUOs02h0sRg+sBriJASMuOkQlkIdNgNVZj0fotlkI4Xcto9dgcjmiThjzt0ruy8QiZUSMK93l95ot-sIhkCQbyJlFIW0YZL8YjQMyruL1dLCS9SUA

💻 Code

type Type = {
  id: number;
};

type InferenceInfo = {
  typeParameter: Type;
  impliedArity?: number;
};

declare function getInferenceInfoForType(type: Type): InferenceInfo | undefined;

function fn1(t1: Type, t2: Type) {
  let info = getInferenceInfoForType(t1);
  if (info?.impliedArity !== undefined) {
    info.impliedArity; // ok
  } else if ((info = getInferenceInfoForType(t2))?.impliedArity !== undefined) {
    info.impliedArity; // 'info' is possibly 'undefined'.(18048)
  }
}

function fn2(t1: Type, t2: Type) {
  let info = getInferenceInfoForType(t1);
  if (info?.impliedArity !== undefined) {
    info.impliedArity; // ok
  } else if ((info = getInferenceInfoForType(t2))?.impliedArity) {
    info.impliedArity; // ok
  }
}

🙁 Actual behavior

The second branch in fn1 contains an error

🙂 Expected behavior

I'd expect this to be narrowed just fine - like the first branch and the second branch in fn2

Additional information about the issue

No response

Guida contributor