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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ts report a wrong comparison when a variable may be changed async. #57927

Closed
HuberTRoy opened this issue Mar 25, 2024 · 3 comments
Closed

Ts report a wrong comparison when a variable may be changed async. #57927

HuberTRoy opened this issue Mar 25, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@HuberTRoy
Copy link

HuberTRoy commented Mar 25, 2024

馃攷 Search Terms

searched This comparison appears to be unintentional because the types ... have no overlap.ts(2367)

馃晽 Version & Regression Information

ts: 5.3.2

馃捇 Code

  let acceptOrCancel: 'pending' | 'cancel' | 'pass' = 'pending';
  

  // I use antd Modal.
  Modal.confirm({
   ....
    onOk: () => {
      acceptOrCancel = 'pass';
    },
    onCancel: () => {
      acceptOrCancel = 'cancel';
    },
  });

  await waitForSomething({ func: () => acceptOrCancel !== 'pending' });
  
 // This comparison appears to be unintentional because the types '"pending"' and '"cancel"' have no overlap.ts(2367)
  if (acceptOrCancel === 'cancel') {
    return {
      status: false,
      msg: 'user reject sign',
    };
  }

馃檨 Actual behavior

Modal.confirm will make a modal for user to ask if accept or not, It's async actually, but the function will return immediately. I have declare the type of the variable possible values and wait until the variable change, but Ts also report the error This comparison appears to be unintentional because the types '"pending"' and '"cancel"' have no overlap.ts(2367)

馃檪 Expected behavior

Ts don't report the error.

Additional information about the issue

No response

@HuberTRoy HuberTRoy changed the title Ts report a wrong comparison when the a variable may be changed async. Ts report a wrong comparison when a variable may be changed async. Mar 25, 2024
@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 25, 2024

Another duplicate of #9998.

See also this issue template:

TypeScript assuming the wrong type either after a callback runs, or within a callback

@HuberTRoy
Copy link
Author

@MartinJohns

So it's expected? Close the issue or not?

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 25, 2024

It's working as intended, yes. You can either close it yourself, or a TypeScript team member will mark this issue as duplicate and the bot will close it (or they close it directly).

A workaround is to use a type assertion to prevent the initial narrowing upon initialization: let acceptOrCancel = 'pending' as 'pending' | 'cancel' | 'pass'

Also, if possible (and here it clearly is), provide code example that can run on its own, and don't omit the Playground link. Your provided code does not compile as-is.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants