LuaLS / LuaLS/lua-language-server
Type narrowing for unioned classes
還沒有人認領這個 Issue。
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 442
- PR 合併指標
- 30 天內沒有已合併 PR
描述
I have a large number of classes that look something like this:
```lua
---@class Foo
---@field type "foo"
---@field name string
---@field property string
-- more unique fields
---@class Bar
---@field type "bar"
---@field name string
---@field stuff string
-- more unique fields
-- more classes
```
I have a function that takes an array of any of these classes as a parameter, so naturally I've unioned all of the classes.
```lua
---@alias Prototypes
---|Foo
---|Bar
-- all of the other classes
---@param prototypes Prototypes[]
local function extend(prototypes)
-- stuff and things
end
```
However, when I try to make `prototypes` a table literal, I get autocomplete for the other classes because the union operator behaves like intersection instead of union.

Is there any way to get it to show only the fields of the class indicated by the `type` field? I can't seem to find way to do a proper union of types.
`@type` seems to only work on variables, and even if it did work on table literals, it would be cumbersome to write out the type for every entry in the array.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先重現此 issue 中帶註解的 Foo/Bar Prototypes[] 範例,並檢查 language server 的聯合型別縮小與表格字面值補全行為。完成的標準是:依據 type 欄位縮小後的可辨識聯合型別,在補全中只呈現所選類別的欄位,同時保留對其他聯合成員的有效補全。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- developer-experience, tooling
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100