-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
Code
I've got the following code:
interface MyGenerator {
nextItem(): null | string;
throwError(): never;
}
type GeneratorMapperFn = (gen: MyGenerator) => string | null;
const mapTokenFunction = (fn: GeneratorMapperFn) => {
return (gen: MyGenerator): string | null => {
return fn(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.
const mappedFunction = mapTokenFunction(gen => {
const next = gen.nextItem();
if (!next) {
gen.throwError();
}
// TS2531: Object is possibly 'null'
return next.toUpperCase();
});It gets even more strange, if I replace the gen.throwError() with a throw "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 be null after the conditional statement.
Actual behavior:
It throws TS2531: Object is possibly 'null'.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed