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

Type inference fails when using combined boolean variable in if condition #55577

Closed
ddubrava opened this issue Aug 30, 2023 · 4 comments · Fixed by #56173
Closed

Type inference fails when using combined boolean variable in if condition #55577

ddubrava opened this issue Aug 30, 2023 · 4 comments · Fixed by #56173
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

@ddubrava
Copy link

🔎 Search Terms

"typescript destructuring losses types", "typescript destructuring losses type object is possibly undefined", "typescript type alias destructuring loses type", "typescript inference with union types"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAqgzhAigVwgJxAJQnZAbYAHgBUA+KAXigG8AoKBqASzgGVkBjDnOALigBmAQzwIA3PUYATIcCH9kAOykQBTRRCkSAvlAA+NSQxbsuPfsDSoJjKDLn9itbRNoClHYEwD2iqMgQUdBAACgBKfngkVAxsXAJCRWQAWwAjdHI6WzQIYGQ0PyzbRhNObjg+QREEABojYvt5f2VVdU062xdnWloOXzhgGjtZJsaARhrmNjLzKdNyuDGoXSoA6ODwiT7FAaHG-kaAJknSswr+U4XD5cp-QJjQsK3+wephh3ehAGYT6bPKy48L43Vb3DZPHoAekhUAAkoMAO7eNAAazgUCE6IgAA9IJ5NLQmAIoCFARUlgAyClzGYVa5Umn-L5hQy2cYAOmA3gAorjfBBFF4RJt6kdOTy+RpBUxhRC2SMvuLeWB+dLZToettdkIcvMeLcyYsoAzDfTqYavq5oXDBlJvDhFAByRHIlFQAAW6AgkwR7qYeGgTGAjvRACsAoMhFA+ml1LIfH5vMSQ7SEHBHYTiSEdRA9RUWUUGByucrVUK8CL5XJDkrJQLy5XpArayqpQ2IdogA

💻 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();
const { data: data2, isSuccess: isSuccess2 } = useQuery();
const { data: data3, isSuccess: isSuccess3 } = useQuery();

// It works as expected
if (isSuccess1 && isSuccess2 && isSuccess3) {
    data1.toExponential();
    data2.toExponential();
    data3.toExponential();
}

const areSuccess = isSuccess1 && isSuccess2 && isSuccess3;

// It doesn't work here, while it's just a combination of 'successes'
if (areSuccess) {
    data1.toExponential();
    data2.toExponential();
    data3.toExponential();
}

🙁 Actual behavior

The issue occurs when using the combined boolean variable areSuccess in the second if condition. Although isSuccess1, isSuccess2, and isSuccess3 are all true, TypeScript cannot narrow down the types of data1, data2, and data3 within the if block, leading to a type error

🙂 Expected behavior

areSuccess is a combination of the individual isSuccess variables, it should correctly infer the types of data1, data2, and data3 as the specific data type (number) defined for the queries. This should allow the code to call the toExponential method on each data variable without any issues.

Additional information about the issue

No response

@ddubrava ddubrava changed the title Type inference doesn't work when using combined boolean variable in if condition Type inference fails when using combined boolean variable in if condition Aug 30, 2023
@fatcerberus
Copy link

For performance reasons, IIRC indirect narrowing only works one level down.

@andrewbranch
Copy link
Member

I think #44730 and #46266 aren’t talking to each other. I’m not sure if that’s a known limitation. @ahejlsberg?

@fatcerberus
Copy link

I thought it was just that

const typeGuard1 = ...
const typeGuard2 = ...
const check = typeGuard1 && typeGuard2;
if (check) {
    // not a type guard
}

doesn't work in general. But maybe I'm wrong about that, I haven't actually tested it thoroughly.

@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 Sep 13, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 13, 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
Development

Successfully merging a pull request may close this issue.

4 participants