The best way for me to ask this question is to give an example.
Let's say I want to implement a method which takes three parameters, like so:
customFilter<T>(values: T[], filterString: string, propName: string) any[] {
return values.filter((value) => value[propName].includes(filterString));
}
In this case, I want to ensure that T is a type which has a property propName which evaluates to a string (or, at the very least, something which implements an includes method).
I've considered writing an interface to use as the first parameter type, but I feel that is just my C# background peeking through. Since the interface would require me to, essentially, hardcode the property name, I don't think that's the right approach.
I know it involves the use of keyof.
I have worked around the issue by doing a quick check in the method:
typeof(values[propName].includes) === 'function'
That, though, feels too "JavaScripty", and, again, I feel like there's probably something in TypeScript that can do this for me.
I know doing something along the lines of this answer would also work, but that still feels very JavaScript.