I have the below definition
interface IAutoCompleteInputProps<T> {
textMember: keyof T;
imageMember: keyof T;
data: T[];
}
But I also want to ensure the value of T[textMember]
must be string
, too.
How can I achieve that?
=== Update:
After I implemented the @R Pasha's answer, when I try to access the textMember
by a.data[a.textMember]
(I expect it will be a string) but the IDE says that
(property) value: T[{ [K in keyof T]: T[K] extends string ? K : never; }[keyof T]]
Type 'T[{ [K in keyof T]: T[K] extends string ? K : never; }[keyof T]]' is not assignable to type 'string'.
Type 'T[T[keyof T] extends string ? keyof T : never]' is not assignable to type 'string'.
Type 'T[keyof T]' is not assignable to type 'string'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'.
Type 'T[string]' is not assignable to type 'string'.ts(2322)
I can cast it to string, but just want to know why? Thanks
PS the code that I got error
function Application<T>(props: IAutoCompleteInputProps<T>) {
const x = props.data[0][props.textMember]; // x here is not string
}
Here is Playground Link