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

Mapping a generic type should again result in a generic type #48036

Closed
5 tasks done
DanielSWolf opened this issue Feb 25, 2022 · 3 comments
Closed
5 tasks done

Mapping a generic type should again result in a generic type #48036

DanielSWolf opened this issue Feb 25, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@DanielSWolf
Copy link

Suggestion

πŸ” Search Terms

  • mapped type
  • type mapping
  • generics
  • generic function
  • unknown

βœ… 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

Performing type mapping on a generic type should result in a new generic type with that mapping applied, rather than a concrete type with unknown in place of generics.

πŸ“ƒ Motivating Example

Consider the following utility type Async<T>. It takes an arbitrary function signature and returns the signature of an equivalent asynchronous function.

type Async<T extends (...args: any) => any> = T extends (...args: infer Args) => infer Result
  ? (...args: Args) => Promise<Result>
  : never;

type SimpleFunction = (arg: number) => string;
type AsyncSimpleFunction = Async<SimpleFunction>;
// result: (arg: number) => Promise<string> βœ…

Now let's give it a generic function:

type GenericFunction1 = <T>(arg: T) => string;
type AsyncGenericFunction1 = Async<GenericFunction1>;
// expected: <T>(arg: T) => Promise<string>
// actual: (arg: unknown) => Promise<string> ⛔️

type GenericFunction2 = <T>(arg: number) => T;
type AsyncGenericFunction2 = Async<GenericFunction2>;
// expected: <T>(arg: number) => Promise<T>
// actual: (arg: number) => Promise<unknown> ⛔️

As this example shows, we cannot use the helper type Async<T> with a generic function signature. If we try, we get a non-generic function signature with unknown wherever a generic argument was used.

πŸ’» Use Cases

We maintain a plugin system. Plugins are developed against a carefully designed API. For every type in the plugin API, there is a corresponding internal type. Plugin values and internal values are identical at runtime, but their types differ. For instance, the same concept may be represented by a class internally, but by a branded interface towards the plugins.

To convert between an internal type and its corresponding plugin type, we use a recursive mapped type. This words great, unless the value is (or contains) a generic function.

@MartinJohns
Copy link
Contributor

I think you want HKT like #1213 or #44875.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 26, 2022
@DanielSWolf
Copy link
Author

@MartinJohns @RyanCavanaugh I'm not quite sure this is a duplicate. As far as I understand, both #1213 and #44875 propose to introduce new syntax for new kinds of types. This proposal, however, simply suggests that existing syntax should produce more consistent results.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants