LuaLS / LuaLS/lua-language-server

[Feature Request] Intersection types

未關閉
#3,152 3 則留言 4 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Lua
星號
4.4k
分支
442
PR 合併指標
30 天內沒有已合併 PR

描述

I apologize if this request has been reported before, I couldn't find any.
It would be great to be able to explicitly differentiate between union types and intersection types
e.g.
```lua
--- @class A
local a = {foo = 1}

--- @class B
local b = {bar = "hello"}

--- @param aOrB A | B
--- @param aAndB A & B
local function someFunc(aOrB, aAndB) end

someFunc(a, a) -- no warning, but 2nd argument should give a warning

--- @param a A
local function someOtherFunc(a) end

--- @type A | B -- would prefer to use A & B and not have any warnings
local aAndB = {foo = 1, bar = "hello"}
someOtherFunc(aAndB) -- Cannot assign `A|B` to parameter `A`. - `B` cannot match `A` - Type `B` cannot match `A` Lua Diagnostics. (param-type-mismatch)
```

While it's possible to use an explicit class with inheritance as a workaround, this is not always viable, especially when using dynamic types with generics
```lua
--- @class C: A, B
local c = {foo = 1, bar = "hello"}
someOtherFunc(aAndB) -- no warning

-- not viable when using something like this
--- @generic T1, T2
--- @param tbl1 T1
--- @param tbl2 T2
--- @return T1 | T2 -- would like to use T1 & T2 instead to avoid the warnings
local function merge(tbl1, tbl2) end

someOtherFunc(merge(a, b)) -- Cannot assign `A|B` to parameter `A`. - `B` cannot match `A` - Type `B` cannot match `A` Lua Diagnostics. (param-type-mismatch)
```

The suggested syntax `A & B` for intersection types and `A | B` for union types matches several other languages, such as TypeScript and PHP.

Edit: another workaround is to enable [luals.github.io/wiki/settings#typeweakunioncheck](https://luals.github.io/wiki/settings/#typeweakunioncheck). This has its own downsides, but may be helpful for people trying to avoid the `param-type-mismatch` warnings

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

未指定實作檔案或測試。首先追蹤 Lua 註解如何表示聯集型別,以及 param-type-mismatch 診斷如何處理它們,然後將行為與 A & B、A | B 和泛型合併範例進行比較;當交集註解避免所示的誤報,同時聯集型別保留目前的檢查時,即表示完成。

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

評估

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

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

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