Description
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows
What is the issue affecting?
Annotations, Type Checking, Completion
Expected Behaviour
In the following example:
---@class A
---@field x integer
---@alias W string|A
---@type W[]
local arr1 = {
'hello',
{
x = 'string', -- assign-type-mismatch
},
3, -- assign-type-mismatch
}
---@type (string|A)[]
local arr2 = {
'hello',
{
x = 'string' -- no warnings
},
3, -- no warnings
}
I'm wondering why arr1
(using alias) and arr2
(using parenthesis) work differently. I expected to have completion and error checking for A
array elements in arr2
the same way I get them in arr1
. In other words, I should get assign-type-mismatch
diagnostic when trying to set x
to 'string'
in arr2
, and also for trying to have a number element (3
) in an array of string|A
.
Actual Behaviour
I don't get any completion suggestions or diagnostics while setting the value for the arr2
case (union of multiple types).
However, if I use arr2
later I do get type checking and completion:
for k, v in ipairs(arr2) do
v.x = 1 -- v is correctly inferred as string|A
end
local v = arr2[2] -- v is correctly inferred as string|A
arr2[3].x = 'hi' -- assign-type-mismatch
so the language server does understand (string|A)[]
syntax in certain cases.
Reproduction steps
Try the code sample I posted.
Additional Notes
I can just use aliases, but sometimes I only need these union types for a parameter or a single location, and an alias introduces a new name globally.
Log File
No response