-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add method
helper
#202
Add method
helper
#202
Conversation
| [T, T, T, T, T] | ||
| [T, T, T, T, T, T] | ||
| [T, T, T, T, T, T, T] | ||
| [T, T, T, T, T, T, T, T]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's common to support 10 when doing this kind of thing.
@@ -119,6 +119,20 @@ checkPassword('foo'); | |||
//=> ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo` | |||
``` | |||
|
|||
### ow.method(predicates, label, body) | |||
|
|||
Wrap a function with parameter validation. Useful for writing strongly-typed functions which will be called with untrusted input. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be useful to elaborate a little bit more on the use-case. As a user, I'm not entirely sure still when I would use it.
*/ | ||
method<Arguments extends Tuple<any>, Return>( | ||
predicates: Extract<{[K in keyof Arguments]: BasePredicate<Arguments[K]>}, any[]>, | ||
functionName: string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need an overload here? Can't we just make this parameter optional?
Looks like a useful utility to me. |
What do you think about the name |
Bump :) |
@sindresorhus sorry, I don't think I'll find the time anytime soon to take this over the line, so closing. Feel free to use as a reference for a future implementation though/if you want to make an issue for this idea. |
This is something I've wished existed for a while. The problem being: I'd like to write a function with specific input types, which is going to be invoked from call-sites outside the project I'm working in (e.g., a library function, an http request/serverless function handler/some json file processor etc.).
But I'd also like to be able to call it internally - the simplest use case being from a unit test.
Example:
Note that
a
andb
are strongly typed (as numbers in the above example) in each function.Previously the options aren't that great. You can either be safe by typing all inputs as
unknown
, which makes sure that the validation is done, but offers no help to callers or the function:Or you can type them as
number
, which provides types to callers, but means that allows skipping the assertion at the type level, leading to errors at runtime if bad data is passed to the function:Or, you can write a separate type signature, leading to confusing/unnecessary boilerplate:
Instead of:
you would have to do:
Curious to hear any thoughts on the idea!
Update :I've since noticed similar functionality in zod.