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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer:9 function pipe #866

Closed
wants to merge 2 commits into from
Closed

Conversation

LMFinney
Copy link
Sponsor Contributor

@LMFinney LMFinney commented May 8, 2024

Checklist for challenge submission

  • Start your PR message with Answer:${challenge_number}

Warning:

  • If you want feedback or review, you must support the project on GitHub:

Alternatively, you can still submit your PR to join the list of answered challenges or to be reviewed by a community member. 馃敟

@github-actions github-actions bot added 9 exercice wrapFn pipe answer answer sponsor sponsor the project to be reviewed PR requests a review labels May 8, 2024
...args: unknown[]
): Result;

transform<Result>(
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting this basic function was the easy part. When I saw that type safety was required, I hoped there was a way to use https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype to handle this, but nope. Ugh.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch, lots of function override.

You can do shorter 馃槄

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw your solution had one less override (resorting to any[] earlier). Is that what you mean? Or another idea?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export class WrapFnPipe implements PipeTransform {
  transform<R, F extends (...args: any[]) => R>(
    func: F,
    ...args: Parameters<F>
  ): R {
    return func(...args);
  }
}

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but that loses all type-safety, right? I think I had that at first, but thought we were supposed to impose type safety.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have type safety with that, I need to retry it, but I'm sure you have it.

Copy link
Sponsor Contributor Author

@LMFinney LMFinney May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, and you're right. There were two parts I didn't catch when I just read your comment:

  1. Using Parameters<F> is the key - it infers the args, and I missed that.
  2. The args have to be any[] instead of unknown[]. Using unknown causes a compilation error. I wouldn't have predicted that.

Thanks

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, never[] also works, and linting doesn't mind that.

@tomalaforge tomalaforge removed the to be reviewed PR requests a review label May 9, 2024
Use `never` and `Parameters` to get the same type safety
Copy link

This pull request is stale because it has been open for 15 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
9 exercice wrapFn pipe answer answer sponsor sponsor the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants