-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generator .throw() causes a "Object is possibly 'undefined'" error during compilation with strictNullChecks is enabled #14431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The error is resolved if I do wrap the if (gen.throw) {
gen.throw(new Error("err"));
} Is there a valid case where the throw function would not exist on a generator ? |
I'll raise this as a question on SO rather than a bug here. interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
} |
I dislike this behavior in TypeScript. All generators can raise an exception and all of them do return something. Instead, why not make generators return a new interface that extends Iterator? This way, we can make
True, the ECMA2015 schema says:
However, I'd argue that an extension of Iterator still conforms to Iterator! This change would be less confusing for newcomers using
|
TypeScript Version: 2.1.1
Code
Compiled with the command
tsc --strictNullChecks --target ES6 example.ts
Expected behavior:
Compilation passes successfully.
Actual behavior:
example.ts(8,1): error TS2532: Object is possibly 'undefined'.
The error is on the gen.throw() line.
I have already tried putting undefined checks, assigning a local variable, trycatch blocks etc in the code but the error persists.
The text was updated successfully, but these errors were encountered: