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

Optional chaining type narrowing on parenthesized assignment expression #60855

Open
mjy9088 opened this issue Dec 26, 2024 · 1 comment Β· May be fixed by #59144
Open

Optional chaining type narrowing on parenthesized assignment expression #60855

mjy9088 opened this issue Dec 26, 2024 · 1 comment Β· May be fixed by #59144

Comments

@mjy9088
Copy link

mjy9088 commented Dec 26, 2024

πŸ”Ž Search Terms

optional chaining type narrowing

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8mwEsD2A7KBeKBvKpIC4oAiAZSQFsIiAaKANwEMAbAVwkJRfICMIAnKAF8oAH2y5w7YgDlUVIQG4AUEoAmEAMZMGfaADMWKDYlRQAzhQhwTKABQBKQteQplSg0ZtQA7gmAALJBZgAFUUdT0EFAhVBw4uXgExQwiomOwlKCgmCGAoJHgXJ0LUNyyAenKfJD4AazNMqAQ9KFtbAq9MC0pnVAd7ADo8aHRRqAByMkpx+wysrN1gFj40DpcBxlYIZSzBFQXc5bQUiEjo1WU9tU1tXSgPYxdzS16UZle42BK0ZPDTtIuKgeXjAfCQ3By5AYiA0n04PH4oigJzO6SwjRyeTWqGKXl+qXOZSglSgKCQeW8NXqjWarXa3wwzx633e336AH4hpIMGNJpYZnN5osjvlvhtmGwdkJ9sSqpS6g0sti0F0XiymB97FLafSbJzhjzMHzprN0fMoMKVqKbOKtlKrgcllaUQDLkogA

πŸ’» Code

type Option = { type: "Some", value: number } | { type: "None" };

declare function someOption(): Option;

function withoutUndefined(): number | undefined {
  let option: Option;

  // works
  if ((option = someOption()).type === 'Some') {
    return option.value;
  }

  return undefined;
}

declare function someOptionalOption(): Option | undefined;

function problematic(): number | undefined {
  let option: Option | undefined;

  // not works
  if ((option = someOptionalOption())?.type === 'Some') {
    return option.value;
  }

  // works
  option = someOptionalOption();
  if (option?.type === 'Some') {
    return option.value;
  }

  return undefined;
}

πŸ™ Actual behavior

Error 'option' is possibly 'undefined'. occurs

πŸ™‚ Expected behavior

No error

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Duplicate of #56998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants