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.
1
The approach you’re using is already clean and idiomatic for TypeScript, especially with generic functions. If the goal is to avoid redundant type definitions, the below approach is likely the cleanest without losing type safety.
You can define getId with a direct assertion that its type matches IdGetter with the Inline Type Assertion approach:
export const getId: <T>(x: Identifiable<T>) => IdType = x => x.id;
Hope this helps!
santhosh prem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.