[Feature request] Annotating function params with ... before last arg
See original GitHub issueHi,
I was wondering if it would be possible to support annotating functions taking n variadic args then a known fixed type.
Basically I have this utility function, which unfortunately can’t be perfectly annotated AFAIK:
(ignore the lack of @generic
, that’s another topic for now, but see the bottom)
--- Convert the function taking a callback into a function returning a promise.
--- The function to wrap must take a callback as last parameter.
--- The callback must take an error as first parameter, or nil if no error, and the result as second parameter.
function promisify(wrappedFn) ... end
What I tried and what would seem to be the optimal annotation but isn’t supported (with ...
or ...:any
):
--- @param wrappedFn fun(..., cb:fun(nilOrErr:nil|string, result:any)) The function to wrap.
--- @return fun(...):Promise The function converted to return a promise.
What I ended up doing for now, which is the best I could find:
--- @param wrappedFn fun(arg:any, cb:fun(nilOrErr:nil|string, result:any)) The function to wrap.
--- @return fun(...):Promise The function converted to return a promise.
Thanks for the awesome work as usual!
Note that I also tried the following, which seemed like an appropriate usage of @generic
, although that triggers errors in usages of that function, where T isn’t matched to the actual usage I have for some reason. Maybe I should make another specific issue for that though.
--- @generic T
--- @param wrappedFn fun(arg:T, cb:fun(nilOrErr:nil|string, result:any)) The function to wrap.
--- @return fun(arg:T):Promise The function converted to return a promise.
Or even more optimally with that feature request implemented, although it’s even more complex because all the variadic args may have different types… Ideally it would just “forward” the type from the wrapped function to the new one, but that sounds quite complex to annotate.
--- @generic ...anyT
--- @param wrappedFn fun(...:anyT, cb:fun(nilOrErr:nil|string, result:any)) The function to wrap.
--- @return fun(...:anyT):Promise The function converted to return a promise.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Interestingly, TypeScript 4.2 just got the feature to have variadic/rest args in other positions than the end: https://devblogs.microsoft.com/typescript/announcing-typescript-4-2-beta/#leading-middle-rest-elements-in-tuple-types 😁
On the plus side, I totally beat them to the smarter type alias preservation feature 😆