-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Wrong "possibly 'undefined'" assertion #29344
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
This is probably because of the other assertion you have in there causing TS to think the types are unrelated (hypothesis: it synthesizes one asserted-non-null type for |
@Kovensky looks like you're right. public thumbYElementRef = (ref: HTMLElement | null) => {
this.thumbYEl = ref;
const thumbProps = this.props.thumbYProps!;
typeof thumbProps.elementRef === 'function' && thumbProps.elementRef(ref);
}; Has no errors. But I'm still thinking that proper behavior is to have no errors in given example. Dont know about implementation possibility and sadly has no time to read tsc sources. |
You would be correct. An assertion prevents us from tracking refinements to the asserted reference right now (it's kinda a known limitation). As @xobotyi points out, you can usually work around that by storing the asserted value in a temporary and reusing it. |
One bad thing about that workaround - useless variable in generated code. |
@xobotyi aaaaaaand now it's fixed 😉 |
@weswigham a-we-some! |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version:
3.2.2
Search Terms:
Cannot invoke an object which is possibly 'undefined'.
Object is possibly 'undefined'.
Code:
Expected behavior:
Compillation without errror.
Due to previous check
typeof this.props.thumbYProps!.elementRef === 'function'
it can't be undefined.Actual behavior:
Cannot invoke an object which is possibly 'undefined'.
Problem solves if add non-null assertion, but it is unnecessary here!
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: