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
This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards
β― Playground Link
// Welcome to the TypeScript Playground, this is a website// which gives you a chance to write, share and learn TypeScript.// You could think of it in three ways://// - A location to learn TypeScript where nothing can break// - A place to experiment with TypeScript syntax, and share the URLs with others// - A sandbox to experiment with different compiler features of TypeScripttyperankedObject={rank: number|undefined}constfoo: rankedObject[]=[{rank: 1},{rank: 4},{rank: -1},{rank: undefined}]constsortRanks=(element1: rankedObject,element2: rankedObject)=>{if(element1.rank!==element2.rank){if(element1.rank&&!element2.rank)return-1;if(!element1.rank&&element2.rank)return1;// error here saying: element1|2 is possibily undefinedreturnelement1.rank-element2.rank;}}// To learn more about the language, click above in "Examples" or "What's New".// Otherwise, get started by removing these comments and the world is your playground.
typerankedObject={rank: number|undefined}constfoo: rankedObject[]=[{rank: 1},{rank: 4},{rank: -1},{rank: undefined}]constsortRanks=(element1: rankedObject,element2: rankedObject)=>{if(element1.rank!==element2.rank){if(element1.rank&&!element2.rank)return-1;if(!element1.rank&&element2.rank)return1;// error here saying: element1|2 is possibily undefined, which is not possiblereturnelement1.rank-element2.rank;}}
π Actual behavior
I have an error saying element1.rank and element2.rank could be undefined
Which is not possible given:
1 - they are not the same
2 - we rulled out (!A && B) and (A && !B)
so the only possibility that left is that A and B are both not undefined
π Expected behavior
no error, given rank are both defined at this point
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
Typeguard narrowing undefined conditions
π Version & Regression Information
β― Playground Link
Workbench Repro
π» Code
π Actual behavior
I have an error saying element1.rank and element2.rank could be undefined
Which is not possible given:
1 - they are not the same
2 - we rulled out (!A && B) and (A && !B)
so the only possibility that left is that A and B are both not undefined
π Expected behavior
no error, given rank are both defined at this point
The text was updated successfully, but these errors were encountered: