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
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:
assertAThing(bla2);yield;
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
generator, yield, assertion function
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The variable
bla2
is stillunknown
even after passing it through the assertion functionassertAThing
.π Expected behavior
bla2
should be narrowed to beSomeType
likebla1
.Workaround
We are effectively yielding
void
since assertion functions cannot return anything. This means there is a simple workaround:The text was updated successfully, but these errors were encountered: