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
{{ message }}
Repository navigation
Yielding an assertion function call does not narrow the asserted typeΒ #55205
function*testFunc(){typeSomeType={a: number};letcount=0;functionassertAThing(obj: unknown): asserts obj is SomeType{// don't care about the implcount++;}constbla: unknown={a: 3};assertAThing(bla);// no type error hereconsole.log(bla.a);constbla2: unknown={a: 4};yieldassertAThing(bla2);// bla2 is unknown, but should be SomeType as asserted by the assertAThing call.if(bla2.a!==4){thrownewError("bla2.a should be 4.");}if(count!==2){thrownewError("Count should be two.");}console.log("Javascript success")}for(constitemoftestFunc()){//}
π Actual behavior
The variable bla2 is still unknown even after passing it through the assertion function assertAThing.
π Expected behavior
bla2 should be narrowed to be SomeType like bla1.
Workaround
We are effectively yielding void since assertion functions cannot return anything. This means there is a simple workaround:
Activity
MartinJohns commentedon Jul 30, 2023
This is not a bug as per #32695:
RyanCavanaugh commentedon Aug 1, 2023
This doesn't seem like something you would likely (ever?) actually do in real code, and making new CFA graph nodes slows down everything else.
typescript-bot commentedon Aug 4, 2023
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.