You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Typescript Nightly produced more weird results: Results in TSNext (Note the string and number should give error, but they don't)
💻 Code
functioniReceiveNumber1(a: number,b: number){console.log(a+b);}functionproduceNumber(): number|undefined{returnMath.random()>0.5 ? 1 : undefined;}/** * Example 1 that works, but lots of code duplicates */conste1: number|undefined=produceNumber();conste2: number|undefined=produceNumber();typeofe1==='number'&&typeofe2==='number'&&iReceiveNumber1(e1,e2);/** * Example 2 that does not work, but I expect it to */conste3: number|undefined=produceNumber();conste4: number|undefined=produceNumber();conste3AndE4Exist=typeofe3==='number'&&typeofe4==='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.
The text was updated successfully, but these errors were encountered:
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. 👍
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
andnumber
should give error, but they don't)💻 Code
🙁 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 ine3AndE4Exist
variable right before I call theiReceiveNumber
function.I also had hard times opening Typescript Playground, some requests failed. I don't know if that is my connection problem or not.
The text was updated successfully, but these errors were encountered: