I have the following function:
export const sortAlphabetically = <T>(array: T[], property: string) =>
array.sort((a: T, b: T) =>
a[property].localeCompare(b[property]));
The property should be a key in T (as string?), no other values should be accepted. I tried with property: [key in t] but this doesn't work.
Is there a way to do this?
keyofoperator should do the trickDocs
You need to assure TypeScript that value of property is
string. That's why I have usedT extends Record<string, string>