Recently, I created an interface following the guidelines mentioned in this thread Defining TypeScript variable type function or string
The interface definition is as follows:
type displayWithFn<T> = (value: T) => string;
export interface Value<T> {
value: T,
displayValue: string | displayWithFn<T>;
}
Within my codebase, there's a function being used:
getDisplayValue(item: Value<T>): string {
if (typeof item.displayValue === 'string') {
return item.displayValue as string;
} else {
return item.displayValue(item.value);
}
}
However, on executing the line item.displayValue(item.value)
, an error is thrown stating "TS2349 cannot invoke an expression whose type lacks a call signature. Type string | displayWithFn has no compatible call signatures."
I am currently using TypeScript version 3.5.3.
Could anyone offer a solution to this problem?