I need simple type checking based on object property which I'd like to use like this:
type User = { id: string, name: string }
sort<User>("name");
where Intellisense offers me "name" or "id" for input or shows "error" if anything else is entered.
With my current implementation property is not of type string, although I'm able to pass only string value.
sort<T extends { [key: string]: any }>(property: keyof T) {
// how can I make 'property' string ???
// required API object is e.g. { property: "Id", desc: true }
}
Here is playground.
propertytype is union of keys from T and it looks fine. Why do you want to have property with typestring?sort<T extends { [key: string]: any }>(property: string) {this way ?sort<T extends { [key: string]: any }>(property: keyof T | string) {