interface A {
a: () => boolean;
}
class AImpl implements A {
public a(): boolean {
return true;
}
}
it('should fail', function () {
const aImpl = new AImpl();
if (aImpl.a) { // TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
console.log("always true");
}
});
it('why success', function () {
const aImpl = new AImpl();
if (!aImpl.a) { // nothing happens here
console.log('always false');
}
});
why aImp.a reports TS2774, but !aImpl.a works well?
And is there any way to find the property-dereference on a method declaration?
As noted by Ramesh Reddy in the comments, if you call if(aImpl.a), it will always return true because you defined aImpl.a. You essentially convert the reference to this function to boolean that will always be true because the function is defined.
What you likely wanted to do is to call the function that you implemented.
In other words, if (aImpl.a()).