Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to parametrize generic function type #44521

Open
5 tasks done
Gozala opened this issue Jun 9, 2021 · 3 comments
Open
5 tasks done

Ability to parametrize generic function type #44521

Gozala opened this issue Jun 9, 2021 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Gozala
Copy link

Gozala commented Jun 9, 2021

Suggestion

I would like to propose something along the lines of $Call utility type in Flow so it is possible to represents the result of calling a generic function with a concrete parameters.

Alternatively could be an utility type that hoists parameters from generic function type into type itself e.g.

HoistTypeParams<<I, O>(inn:I[], f:(p:I) => O) => O[]> // => type DerivedType<I, O> = (inn:I[], f:(p:I) => O) => O[]

Or yet another alternative might be to add some way to parametrize through infer e.g. I would have expected following to work:

type $Call<T extends (args: any[]) => any, Args extends unknown[]> =
  T extends (...args:Args) => infer O ? O : never

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

Today it seems impossible capture return type of the generic function, even when concrete type of the input is known. Both built-in ReturnType and infer based solutions turn generics into a unknowns.

Here is concrete example:

type Map = <I, O> (list:I[], f:(i:I) => O) => O[]

ReturnType<Map> // unknown[]
Map extends (...args:infer Args) => infer O ? O : never // unknown[]
Call<Map, [number[], (i:number) => string]> // unknown[]

type Call<F, Args extends unknown[]> = F extends (...args:Args) => infer O ? O : never

As per #33185 (comment) it is possible to accomplish this at the implementation level, however I do not believe it is possible to do it at the type level.

💻 Use Cases

Libraries that generate more complex interfaces from a simple ones, something like RPC client for a service with generics becomes impossible here to express:

type Service<T, Path extends PropertyKey[]=[]> = {
  [K in keyof T]: T[K] extends (...args:infer I) => infer O ? Method<I, O, [...Path, K]> : Service<T[K], [...Path, K]>
}

interface Method<I extends unknown[], O, Path extends PropertyKey[]> {
  <Args extends I> (...args:Args): Promise<O>
  meta: {  path: Path }
}


declare function service <T>(impl:T):Service<T>
const s = service({
  map <I, O>(inn:I[], f:(inn:I) => O):O[] {
    return inn.map(f)
  }
})

s.map([1, 2, 3], (n:number):string => String(number))
//               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Argument of type '(n: number) => string' is not assignable to parameter of type '(inn: unknown) => unknown'.
//  Types of parameters 'n' and 'inn' are incompatible.
//    Type 'unknown' is not assignable to type 'number'.(2345)
@MartinJohns
Copy link
Contributor

Related: #40542

@Gozala
Copy link
Author

Gozala commented Jun 9, 2021

I think #40542 proposes another way to solve this problem, my only concern is that in some cases you may not know about number of type parameters function takes making it tricky to qualify the type. On the other hand if following inferred return type correctly it would be possible to map functions return type without having to know about it's parameters

type $Call<T extends (args: any[]) => any, Args extends unknown[]> =
  T extends (...args:Args) => infer O ? O : never

type MapReturn<T, F> =
  T extends (...args:infer Args) => any ?
  <In extends Args> (...args:In) => $Call<F, $Call<T, In>> : 
  nevenr

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jun 10, 2021
@ahejlsberg
Copy link
Member

With #47607 it is possible to refer to specific instantiations of generic functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants