You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Class generics produce unusable and wrong union types #3261
AFAIK, the generic support of LuaLS is very limited. 😇
There are many caveats, and the "best" annotation that I attempted to achieve your needs is as follow:
A generic class must inherit a table with a field to store the generic type T
ref: #1532 (comment)
In my example you can see there is a { __hidden: T } there.
If you remove it, then ---@param self Stack<T> annotation in the methods won't work
=> which will cause :Peek() and :Pop() annotation failing to work
caveat 2
A generic type in function annotation can only do type capturing but not type enforcing
i.e. the ---@param value T in Stack:Push actually does nothing
because it can only be used the match the type T in Stack<T>
but cannot be used the enforce value param to be of the captured type T
and therefore LuaLS cannot check against incorrect value type that you pass to :Push()
If you use generic types heavily, you may want to try CppCXY's new language server written in Rust.
It has much better generic class support 👀
Activity
tomlau10 commentedon Sep 2, 2025
AFAIK, the generic support of LuaLS is very limited. 😇
There are many caveats, and the "best" annotation that I attempted to achieve your needs is as follow:
caveat 1
A generic class must inherit a table with a field to store the generic type
Tref: #1532 (comment)
In my example you can see there is a
{ __hidden: T }there.If you remove it, then
---@param self Stack<T>annotation in the methods won't work=> which will cause
:Peek()and:Pop()annotation failing to workcaveat 2
A generic type in function annotation can only do type capturing but not type enforcing
---@param value TinStack:Pushactually does nothingTinStack<T>valueparam to be of the captured typeTvaluetype that you pass to:Push()If you use generic types heavily, you may want to try CppCXY's new language server written in Rust.
It has much better generic class support 👀