LuaLS / LuaLS/lua-language-server

Type narrowing for unioned classes

未關閉
#2,109 2 則留言 6 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

enhancement
主要語言
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.

![image](https://github.com/LuaLS/lua-language-server/assets/88168191/6ec8340a-2921-4a5d-b781-5f36094b8bdb)

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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先重現此 issue 中帶註解的 Foo/Bar Prototypes[] 範例,並檢查 language server 的聯合型別縮小與表格字面值補全行為。完成的標準是:依據 type 欄位縮小後的可辨識聯合型別,在補全中只呈現所選類別的欄位,同時保留對其他聯合成員的有效補全。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
lua
領域
developer-experience, tooling
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。