I have come across this particular definition:
interface IAutoCompleteInputProps<T> {
textMember: keyof T;
imageMember: keyof T;
data: T[];
}
However, I also need to ensure that the value of T[textMember]
is a string
. How can this be accomplished?
=== Update:
Upon implementing @R Pasha's solution, an issue arises when attempting to access the textMember
using a.data[a.textMember]
. The IDE reports:
(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)
The situation can be mitigated by casting it to a string, but the root cause remains unclear. Any insights would be much appreciated.
Please note the snippet of code where the error occurs:
function Application<T>(props: IAutoCompleteInputProps<T>) {
const x = props.data[0][props.textMember]; // At this point, x is not a string
}
For more details and experimentation, you may click on this Playground Link.