Skip to content

A union type should not merge class fields #3410

@CarterLi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions