Description
TypeScript Version: 2.1.5 and 2.1.6
Code
With --strictNullChecks
enabled.
interface Foo {
foo(): void
}
class A<P extends Partial<Foo>> {
props: Readonly<P>
doSomething() {
this.props.foo && this.props.foo()
}
}
Expected behavior:
I should be able to invoke this.props.foo()
as I have proven to the typechecker that this.props.foo
is not undefined
† and it cannot be any other value.
Actual behavior:
Throws the following compiler error:
test.ts(12,23): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '(() => void) | undefined' has no compatible call signatures.
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
is P
instead of Readonly<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 while strictNullChecks
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 potentially undefined
value.
† Technically this code proves to the typechecker that this.props.foo
is not falsey, however the same issue is found when comparing this.props.foo
explicitly to undefined
or doing typeof this.props.foo === 'function'
.