#arg: keyof T and T[arg] is a number
19 messages · Page 1 of 1 (latest)
perhaps something like this?
class MyClass<K extends PropertyKey, T extends Record<K, number>>
then use K
weird pattern though, why do you need something like this
It's a priority queue-like class that automatically sorts items on a given property key. I'm just trying to make that key able to be checked at compile time.
why enforce that it's a number then? i feel you could also do sorting by Date or string or whatever
Is there a way I could avoid having to pass in an additional type argument? Currently, this solution seems to break existing usage of the class.
in that case i feel you could get more mileage by providing a sorting callback, like how sort works
It's used specifically in pathfinding, for keeping track of nodes to visit. The sort key is almost always the total distance from the first node to that node
not that i know of, sorry
Again, this isn't really ideal since it breaks exisiting usage
why have it be generic at all then?
Oh well, thanks for trying - you did better that I managed
It's used slightly differently with different node classes in different places
do those node classes have other properties that you're sorting by?
yeah, the naming varies
I guess I could have an union somwhere of all the different keys, but that just feels like way overcompilcating
i feel sort-like callbacks are the way to go there
yeah that's definitely overcomplicating it
You're right, but again - just trying to tidy up some types here and there, not refactor the actual functional parts of working code