LuaLS / LuaLS/lua-language-server

[Feature Request] Intersection types

Aperta
#3,152 3 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non vengono indicati file di implementazione né test. Inizia tracciando come le annotazioni Lua rappresentano le unioni e come la diagnostica param-type-mismatch le gestisce, quindi confronta il comportamento con gli esempi A & B, A | B e di merge generico; il lavoro è concluso quando le annotazioni di intersezione evitano i falsi avvisi mostrati, mentre le unioni mantengono i controlli attuali.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua
Ambito
developer-experience, tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.