Does TypeScript allow a type alias to specify generics?

Viewed 2410

I would like to be able to alias a very generic function and specify part of the generic parameters, thus creating a less generic version of the same function. Something like the following:

function veryGeneric<X, Y>(someParam: Y): { result: X } {
  // ...
}

type LessGeneric = typeof veryGeneric<X, string>
const lessGeneric: LessGeneric = veryGeneric

Where I would want the lessGeneric function to essentially be typed as:

function lessGeneric<X>(someParam: string): { result: X } {
  // ...
}

Is this possible in any way?

I know I could create a wrapper function, but I would prefer to not have to specify the parameter typings again (and not having to pay the overhead of another function call, even if it’s tiny, would be a bonus).


Here’s the real example I’m dealing with. Given a function declaration (from react-tracking) like the following:

declare function track<T = {}, P = {}>(trackingInfo?: TrackingInfo<T, P>, options?: Options<Partial<T>>): Decorator

I want to be able to define an alias that specifies the trackingInfo parameter’s typing but leaves P generic. i.e. I want an alias that’s essentially typed as:

interface ValidAnalyticsEntries {
  page: string
  action: string
}

declare function trackSpecificToOurAnalyticsSchema<P = {}>(trackingInfo?: TrackingInfo<ValidAnalyticsEntries, P>, options?: Options<Partial<ValidAnalyticsEntries>>): Decorator
2 Answers
Related