Skip to content

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

@Andarist

Description

@Andarist

🔎 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions