I have these three helpers defined:
export type IdType = PropertyKey;
export type Identifiable<T> = T & {id: IdType};
export type IdGetter<T> = (x: Identifiable<T>) => IdType;
I would like to be able to type generic methods using the IdGetter generic, like (this doesn't work, but that's the sort of thing I would like to be able to do):
export const getId<T>: IdGetter<T> = x => x.id
But instead I have to write:
export const getId = <T>(x: Identifiable<T>): IdType => x.id;
It's not a big deal, I can still do things like that and have no type errors:
const f = <U, F extends IdGetter<U>>(getter: F) => (x: Identifiable<U>): IdType => getter(x);
f(getId);
But defining the types of these getters is redundant.
Is there a clean way to do what I'm looking for? I'm trying to get better at using generics.