π 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
π Search Terms
discriminant alias condition binding element parameter narrowing narrow
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.0-dev.20231109#code/C4TwDgpgBAqgzhAigVwgJxAJQnZAbYAHgBUA+KAXgCgooAfKAbxttYEs4BlZAYx5zgAuKADMAhngQBuFqygATMcDHDkAO3kQRbNRHky5AXxYNmc2h258Bw4GlQHzi5cOKPahmVRHqewNgD2alDICCjoIAAUAJTC8EioGNi4BIRqyAC2AEbo5GZQaBDAyGjB+RZcvPxwQqISCAA0sgpKKiEaWjp6TR4yxlQ8QXDATC0uY2IAjA1QllU2s5XWNZNQhpQhYYlR0V4+an6BwcA4wJGME8LOUzNzy7V31XCrhnFbEcn4ROnZudFMLEGamGo2uV1aACZbksnsJHgIIWsNqEEhEYl5aECQWJClYnht4SsoAAyYmLPEIxxsERQSI4iAUmr-coTSYAOmAAQAogAPMBBCBqfwSdFQAD0YqgcAAFgF8PIoDkoAEANbNa4QjncvkCoVsEW7cWSjjKtUeKjGIA
π» Code
π Actual behavior
data1is not narrowed downπ Expected behavior
data1should be narrowed down just likedata2is narrowed downAdditional 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.
isConstantReferencecan'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