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

Simplify protocol map definitions with function types #52

Open
NoamLoewenstern opened this issue Mar 19, 2023 · 2 comments
Open

Simplify protocol map definitions with function types #52

NoamLoewenstern opened this issue Mar 19, 2023 · 2 comments

Comments

@NoamLoewenstern
Copy link
Contributor

Was redirected to this repo after a discussion in a similar repo, realizing where webext-core got inspired from (your repo ;-) )
After opening an issue there and simplifying the ProtocolMap definiotion when using function-types, I would recommend doing the same here.
References to the issue & pull request & change in the other repo, which can be helpful for this repo too:
The original issue
The pull request
The change

And here's a changes I would ask and recommend to modify in types.ts:

export type GetDataType<
  K extends DataTypeKey,
  Fallback extends JsonValue = undefined,
> = K extends keyof ProtocolMap
  ? ProtocolMap[K] extends (...args: infer Args) => any
    ? Args['length'] extends 0
      ? undefined
      : Args[0]
    : ProtocolMap[K] extends ProtocolWithReturn<infer Data, any>
      ? Data
      : ProtocolMap[K]
  : Fallback;


export type GetReturnType<
  K extends DataTypeKey, 
  Fallback extends JsonValue = undefined
> = K extends keyof ProtocolMap
  ? ProtocolMap[K] extends (...args: any[]) => infer Return
    ? Return
    : ProtocolMap[K] extends ProtocolWithReturn<any, infer Return>
      ? Return
      : void
  : Fallback;
@zikaari
Copy link
Collaborator

zikaari commented Mar 24, 2023

Thanks for reporting, this part of the code was entirely written by another collaborator, I don't have a strong grasp on the how's and what's of this mechanism.

Would you be willing to create a pull request with the proposed changes?

@NoamLoewenstern
Copy link
Contributor Author

Thanks for reporting, this part of the code was entirely written by another collaborator, I don't have a strong grasp on the how's

Sure. hopefully today, maybe within the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants