Skip to content

A union type should not merge class fields #3410

Open

Description

@CarterLi

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

MacOS

What is the issue affecting?

Annotations

Expected Behaviour

---@class (exact) TypeA
---@field type 'A'
---@field commonField number
---@field specificFieldA number

---@class (exact) TypeB
---@field type 'B'
---@field commonField string
---@field specificFieldB string

---@alias Type_Either_A_Or_B_But_Not_Both TypeA|TypeB

---@type Type_Either_A_Or_B_But_Not_Both
local testA = {
    type = 'A', --- Indicates that this should be of TypeA
    commonField = 'test', --- Should be an error. TypeA.commonField should be a number
    specificFieldA = 123, --- OK
    specificFieldB = 'Hello' --- Should be an error. TypeA.specificFieldB doesn't exist
}

---@type Type_Either_A_Or_B_But_Not_Both
local testB = {
    type = 'B', --- Indicates that this should be of TypeB
    commonField = 'test', --- OK
    specificFieldA = 123, --- Should be an error. TypeB.specificFieldA doesn't exist
    specificFieldB = 'Hello' --- OK
}

Actual Behaviour

TypeA|TypeB is merged, so none of the mismatched fields/types are reported as errors.

local testA: TypeA|TypeB {
    commonField: string|number,
    specificFieldA: number,
    specificFieldB: string,
    type: 'A'|'B',
}

Reproduction steps

Paste the code into vscode

Additional Notes

No response

Log File

No response

Activity

  1. AlexanderHott commented on May 30, 2026

    @AlexanderHott

    +1

    On 3.18.2 I get the same behavior.

    ---@class (exact) Circle
    ---@field kind "circle"
    ---@field radius number
    
    ---@class (exact) Rect
    ---@field kind "rect"
    ---@field width number
    ---@field height number
    
    ---@alias Shape Circle | Rect
    
    ---@param shape Shape
    local function area(shape)
        if shape.kind == "circle" then
            return math.pi * shape.radius * shape.radius
        elseif shape.kind == "rect" then
            return shape.width * shape.height
        end
    end

    If you hover shape in the "circle" if block, you get

    (parameter) shape: Circle {
        height: number,
        kind: "circle"|"rect",
        radius: number,
        width: number,
    }

    and when you complete for fields with shape., you get width, height, radius, and kind, even though it picks up that shape.height is undefined in that block.

    if shape.kind == "circle" then
        shape.height -- Lua Diagnostics.: Undefined field `height`. [undefined-field]
    end

    I would expect that both completion options and the resolved type get narrowed inside of the if block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions