-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
[Feature request] Annotating function params with ... before last arg #33
Comments
As far as type systems go, any such type would be inherently unsound because it's impossible to know how many arguments are required to call such a function. It sounds more like you're after some meta-type functionality which would allow you to derive new types from other types. In this case, specifically altering a parameter list. You can see some rather creative solutions people have come up with for TypeScript: Whilst this certainly is not impossible to implement, unfortunately, it's honestly far more sophisticated than anything I'm planning. If you've any control over the API in question I'd be inclined to strongly suggesting avoiding writing an API that appends parameters to a parameter list. It seems unnecessarily complicated / error prone, particularly when optional parameters / overloads get involved. Prepending parameters would be much more manageable. |
Alright, thanks for the detailed reply - I don't actually disagree with you, but had a bit of hope just in case :D And yeah unfortunately it's an external lib, as you can see for yourself: https://github.com/luvit/luvit/blob/master/deps/fs.lua (the async functions) |
Was just thinking about this some more. No intent to implement it, however it's probably not quite as unsound as I claimed. At least, it's not so much an issue for callers as it is for implementers. Basically the inferred parameter types aren't very useful: ---@alias WeirdCallback fun(a: string, ...: T, c: string): void
---@type fun(weirdCallback: WeirdCallback): void
local callWeirdCallback
callWeirdCallback(function(a, b, c) -- INFERRED fun(a: string, b: string | T, c: nil | string | T): void
end) Of course you probably wouldn't provide a callback with explicit parameters, but instead you'd use Even if the IDE was to infer Anyway, I just thought this was interesting. It's not impossible to implement, just impractical 😛 |
Right, what they do in TS is quite complex indeed :P |
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 😆 ---@alias BasicPrimitive number | string | boolean
---@param value BasicPrimitive
function doStuff(value)
if (math.random() < 0.5) then
return nil
end
return value
end |
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)What I tried and what would seem to be the optimal annotation but isn't supported (with
...
or...:any
):What I ended up doing for now, which is the best I could find:
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.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.
The text was updated successfully, but these errors were encountered: