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

Typescript does not automatically infer existence & validity of a variable #45194

Closed
aghArdeshir opened this issue Jul 27, 2021 · 4 comments
Closed
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@aghArdeshir
Copy link
Contributor

Bug Report

When I want to check existence of a variable and then do something with it, I have to do it right before the usage, I cannot move it somewhere else and this makes refactoring hard/impossible

🔎 Search Terms

infer, infer automatically

🕗 Version & Regression Information

Typescript: 4.3.5

⏯ Playground Link

Playground link with relevant code

Typescript Nightly produced more weird results: Results in TSNext (Note the string and number should give error, but they don't)

💻 Code

function iReceiveNumber1(a: number, b: number) {
    console.log(a + b);
}


function produceNumber(): number | undefined {
    return Math.random() > 0.5 ? 1 : undefined;
}

/**
 * Example 1 that works, but lots of code duplicates
 */

const e1: number | undefined = produceNumber();
const e2: number | undefined = produceNumber();

typeof e1 === 'number' && typeof e2 === 'number' && iReceiveNumber1(e1, e2);

/**
 * Example 2 that does not work, but I expect it to
 */

const e3: number | undefined = produceNumber();
const e4: number | undefined = produceNumber();

const e3AndE4Exist = typeof e3 === 'number' &&  typeof e4 === 'number';
e3AndE4Exist && iReceiveNumber1(e3, e4);

// I receive error in above line, but I did not expect error

🙁 Actual behavior

I receive error that says Type 'undefined' is not assignable to type number. Although I checked the existence in a variable (e3AndE4Exist

🙂 Expected behavior

I expected TypeScript to automatically infer that the type is not undefined as I checked it in e3AndE4Exist variable right before I call the iReceiveNumber function.

I also had hard times opening Typescript Playground, some requests failed. I don't know if that is my connection problem or not.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 27, 2021

This already works in TypeScript 4.4.0 due to #44730, at least in v4.4.0-beta. This is a duplicate of #12184.

@RyanCavanaugh
Copy link
Member

Typescript Nightly produced more weird results: Results in TSNext

I don't see anything weird in the nightly build, can you explain what you find to be wrong?

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Jul 27, 2021
@aghArdeshir
Copy link
Contributor Author

I'm sorry. I checked again today, and in nightly version everything was still weird. I tried a simple const a: number = 'a' and again, I did not see any error. I think there is something wrong with my connection. By the way all requests from https://typescript.azureedge.net/cdn/4.4.0-dev.20210727/... fail.

I tried with version 4.4.0 locally and the issue is solved and everything was fine. Sorry for the inconvenience. 👍

I think this issue can be closed

@RyanCavanaugh
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants