Skip to content

Wrong missing-fields diagnostic behavior with union types #2252

Closed
@firas-assaad

Description

@firas-assaad

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, Diagnostics/Syntax Checking

Expected Behaviour

Building on #2214 (comment), the following code should work without any warnings:

---@class Foo
---@field a number
---@field b number

---@class Bar
---@field aa number
---@field bb number

---@type Foo|Bar
local fb = {
    aa = 1,
    bb = 2
}

---@param foobar Foo|Bar
local function func(foobar) end

func {
    aa = 1,
    bb = 2
}

Actual Behaviour

I get missing field warning when defining the local variable fb, and when calling the function func: "Missing fields: a, b"

It seems to depend on the order of classes in the union. For example, this works (placing Bar before Foo):

---@type Bar|Foo
local fb = {
    aa = 1,
    bb = 2
}

But the following doesn't:

---@type Bar|Foo
local fb = {
    a = 1,
    b = 2
}

Reproduction steps

Paste the sample code in your editor.

Additional Notes

No response

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions