Typescript Union type with a function
As Titian already mentioned, you have to use parenthesis.
class TimePeriod {
name: string | (() => string);
}
Another approach is to use a type alias:
type ATypeName = () => string;
class TimePeriod {
name: string | ATypeName;
}
You just need an extra set of parenthesis.
class TestClass {
name: string | (() => string);
}
The compiler is trying to do (string | ()) => string
if you don't use them, because of precedence.