-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Cannot invoke an expression whose type lacks a call signature when --strictNullChecks enabled #14091
Comments
Since there is a generic type, the type of |
Hey @mhegazy, regardless of discussions of feature implementations - I submitted this as a bug because it's a regression; this was working perfectly fine in TS 2.1.4. What has changed? I'd also be surprised if we would want a new operator for this; this seems a very intuitive type deduction for TypeScript to be able to make. I wouldn't want to have to circumvent the type checker by forcing it to ignore the possibility of something being undefined. In general I try to tell the compiler what something because I'm usually wrong, so I would be pretty upset if the answer to this was Also worth noting that the above code works perfectly fine without the |
I'm pretty sure that I have the same problem with the newest React definitions that define |
@JabX Yes, this is where I got the issue from. I just simplified the example into something that was, well, simpler. |
So, is this the only workaround or it can be prettier? export interface FormBaseProps {
onInitForm?: (f: FormBase<any, any>) => any
onDestroyForm?: (f: FormBase<any, any>) => any
}
abstract class FormBase<T extends FormBaseProps, S> extends Component<T, S> {
componentWillMount() {
this.props.onInitForm && (this.props as FormBaseProps).onInitForm!(this)
}
componentDidUnmount() {
this.props.onDestroyForm && (this.props as FormBaseProps).onDestroyForm!(this)
}
} |
TypeScript Version: 2.1.5 and 2.1.6
Code
With
--strictNullChecks
enabled.Expected behavior:
I should be able to invoke
this.props.foo()
as I have proven to the typechecker thatthis.props.foo
is notundefined
† and it cannot be any other value.Actual behavior:
Throws the following compiler error:
This issue was introduced in TypeScript 2.1.5 and is present in TypeScript 2.1.6. The above code compiles just fine in TypeScript 2.1.4.
This issue does not present itself if
A.props
isP
instead ofReadonly<P>
. You are unable to circumvent this error by using!
, so this is probably not an error with null checking, despite it only being present whilestrictNullChecks
are enabled.Without wanting to make myself sound more intelligent than I am, I suspect the issue has something to do with the interaction between
readonly
and a potentiallyundefined
value.† Technically this code proves to the typechecker that
this.props.foo
is not falsey, however the same issue is found when comparingthis.props.foo
explicitly toundefined
or doingtypeof this.props.foo === 'function'
.The text was updated successfully, but these errors were encountered: