I am currently working on defining an interface that includes a method with a conditional function definition.
For example:
interface Example<T> {
func: T extends string ? () => string : () => number;
}
class ExampleClass<T extends string> implements Example<T> {
func = () => "Dogs";
}
However, I am encountering an error:
Property 'func' in type 'ExampleClass<T>' is not assignable to the same property in base type 'Example<T>'.
Type '() => string' is not assignable to type 'T extends string ? () => string : () => number'
My goal was for TypeScript to understand that I specifically want () => string
because T
is defined as extending string. Even though I believe I have already narrowed it down, TypeScript seems to be asking me to declare a type that encompasses both conditions of the conditional statement.