LuaLS / LuaLS/lua-language-server

A union type should not merge class fields

Open
#3,410 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### 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

```lua
---@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_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the annotated TypeA|TypeB example in the Visual Studio Code extension, then trace how the language server merges union class fields. Done means the discriminator selects the correct class and reports the mismatched commonField and member-specific fields shown in the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.