Skip to content

Aliased discriminants with declarations in binding elements within parameters are not narrowing #56351

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

Closed
Andarist opened this issue Nov 9, 2023 · 0 comments Β· Fixed by #56860
Closed
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

Andarist commented Nov 9, 2023

πŸ”Ž Search Terms

discriminant alias condition binding element parameter narrowing narrow

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231109#code/C4TwDgpgBAqgzhAigVwgJxAJQnZAbYAHgBUA+KAXgCgooAfKAbxttYEs4BlZAYx5zgAuKADMAhngQBuFqygATMcDHDkAO3kQRbNRHky5AXxYNmc2h258Bw4GlQHzi5cOKPahmVRHqewNgD2alDICCjoIAAUAJTC8EioGNi4BIRqyAC2AEbo5GZQaBDAyGjB+RZcvPxwQqISCAA0sgpKKiEaWjp6TR4yxlQ8QXDATC0uY2IAjA1QllU2s5XWNZNQhpQhYYlR0V4+an6BwcA4wJGME8LOUzNzy7V31XCrhnFbEcn4ROnZudFMLEGamGo2uV1aACZbksnsJHgIIWsNqEEhEYl5aECQWJClYnht4SsoAAyYmLPEIxxsERQSI4iAUmr-coTSYAOmAAQAogAPMBBCBqfwSdFQAD0YqgcAAFgF8PIoDkoAEANbNa4QjncvkCoVsEW7cWSjjKtUeKjGIA

πŸ’» Code

type UseQueryResult<T> =
  | {
      isSuccess: false;
      data: undefined;
    }
  | {
      isSuccess: true;
      data: T;
    };

function useQuery(): UseQueryResult<number> {
  return {
    isSuccess: false,
    data: undefined,
  };
}

const { data: data1, isSuccess: isSuccess1 } = useQuery();

function test({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
  const { data: data2, isSuccess: isSuccess2 } = useQuery();

  const areSuccess = isSuccess1 && isSuccess2;
  if (areSuccess) {
    data1.toExponential(); // should be ok
    data2.toExponential(); // is ok
  }
}

πŸ™ Actual behavior

data1 is not narrowed down

πŸ™‚ Expected behavior

data1 should be narrowed down just like data2 is narrowed down

Additional information about the issue

The fix for this would be an extension to the recently landed #56173 . However, it's not straightforward to fix this right now. isConstantReference can't properly assess if the related parameter symbol is "const" or not and the code operates there on a pseudo reference of the whole binding pattern and not on the individual symbol. It would become much more straightforward if this would land: #56313

@RyanCavanaugh RyanCavanaugh added Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases Help Wanted You can do this labels Nov 9, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
2 participants