question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] Annotating function params with ... before last arg

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
adriwebcommented, Jan 13, 2021

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 😁

0reactions
Benjamin-Dobellcommented, Jan 13, 2021

On the plus side, I totally beat them to the smarter type alias preservation feature 😆

---@alias BasicPrimitive number | string | boolean

---@param value BasicPrimitive
function doStuff(value)
    if (math.random() < 0.5) then
        return nil
    end

    return value
end

Screen Shot 2021-01-14 at 12 49 13 am

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass method argument in Aspect of custom annotation
The only way to get parameter value inside the checkEntity annotation is by accessing the arguments array which is not what I want...
Read more >
Annotating function parameters and return values
Reference guide to function parameter and return value annotations.
Read more >
Annotating JavaScript for the Closure Compiler - GitHub
Used with method, function and constructor definitions to specify the types of function arguments. The @param tag must be followed by a type ......
Read more >
Feature Annotations - Pyre
The via feature indicates that a flow passed through a point in the code, such as a function parameter, that was annotated with...
Read more >
Understanding type annotation in Python - LogRocket Blog
To annotate return value type, add -> immediately after closing the parameter parentheses, just before the function definition colon( : ):
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found