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
interfaceMyGenerator{nextItem(): null|string;throwError(): never;}typeGeneratorMapperFn=(gen: MyGenerator)=>string|null;constmapTokenFunction=(fn: GeneratorMapperFn)=>{return(gen: MyGenerator): string|null=>{returnfn(gen);};};// The error occurs at this "function", TS seems to know that "gen" implements "MyGenerator"// but tells me instead that "next" could be null at the very end.constmappedFunction=mapTokenFunction(gen=>{constnext=gen.nextItem();if(!next){gen.throwError();}// TS2531: Object is possibly 'null'returnnext.toUpperCase();});
I think this is a current limitation.
I don't know where the documentation for this is, but TS needs explicit type annotations for the assert and never returning functions to contribute to control flow analysis.
For now, you can fix this error by annotating (gen: MyGenerator) => ...
simonwep
added a commit
to simonwep/bavary
that referenced
this issue
Jan 3, 2020
Code
I've got the following code:
It gets even more strange, if I replace the
gen.throwError()
with athrow "Foo"
it works:... or if
gen
got an explicit type-assignment ((gen: )
):Although TS tells me that gen is of type

MyGenerator
:Expected behavior:
TS should correctly assume that
next
cannot benull
after the conditional statement.Actual behavior:
It throws
TS2531: Object is possibly 'null'
.Related Issues:
#34795, #9998
The text was updated successfully, but these errors were encountered: