Closed mmkal closed 3 years ago
Looks like a useful utility to me.
What do you think about the name .wrap
instead of .method
?
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.