LuaLS / LuaLS/lua-language-server
[Feature Request] Intersection types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Lua
- Sterne
- 4.4k
- Forks
- 442
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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.
--- @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
--- @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. This has its own downsides, but may be helpful for people trying to avoid the param-type-mismatch warnings
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es werden keine Implementierungsdateien oder Tests genannt. Beginne damit, nachzuverfolgen, wie Lua-Annotationen Vereinigungen darstellen und wie die param-type-mismatch-Diagnose damit umgeht, und vergleiche anschließend das Verhalten anhand der Beispiele für A & B, A | B und generisches Zusammenführen; fertig ist es, wenn Schnittmengen-Annotationen die demonstrierten Fehlwarnungen vermeiden, während Vereinigungen ihre aktuellen Prüfungen beibehalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- lua
- Bereich
- developer-experience, tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100