I'm trying to define an interface that has a method with a conditional function definition.
I.e.,
interface Test<T> {
func: T extends string ? () => string : () => number;
}
class TestClass<T extends string> implements Test<T> {
func = () => "Cats";
}
I get the error:
Property 'func' in type 'TestClass<T>' is not assignable to the same property in base type 'Test<T>'.
Type '() => string' is not assignable to type 'T extends string ? () => string : () => number'`
My intention is that TS should know I want () => string
because T
is declared as extending string. However, it seems to want me to declare a type that covers both sides of the conditional even though (I think) I've already narrowed it.
question from:
https://stackoverflow.com/questions/65947618/defining-an-interface-property-as-the-evaluated-version-of-a-generic-conditional 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…