In my class, there is a property called "isVisible" which can be either a boolean value or a function that returns a boolean.
The code snippet below demonstrates what I am currently using. It works fine and achieves the desired result, but during compilation, I keep encountering the error message:
error TS2349: Cannot invoke an expression whose type lacks a call signature.
export class Foo {
isVisible: boolean | ((bar?: any) => boolean);
constructor(isVisible: boolean | ((bar?: any) => boolean) = true) {
this.isVisible= isVisible;
}
}
When evaluating instances of Foo
, I use the following method to check if each one is visible:
public isVisible(item: Foo) {
if (typeof item.isVisible === 'boolean') {
return item.isVisible;
}
return item.isVisible(this.thing);
}
Edit:
The compilation error is specifically pointing to the last line in the code block above (
return item.isVisible(this.thing);
).
This error is generated by the gulp typescript plugin when running the build task.
Edit 2:
It seems that the issue might be due to still using TypeScript 1.8 - I will attempt to upgrade to version 2.x to see if it resolves the problem.