Skip to content
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

Completion/Type checking arrays of union types #1928

Closed
firas-assaad opened this issue Feb 19, 2023 · 0 comments
Closed

Completion/Type checking arrays of union types #1928

firas-assaad opened this issue Feb 19, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@firas-assaad
Copy link
Contributor

firas-assaad commented Feb 19, 2023

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

@sumneko sumneko added the bug Something isn't working label Feb 20, 2023
@sumneko sumneko closed this as completed in 7ef36fa Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants