LuaLS / LuaLS/lua-language-server
[Feature Request] Intersection types
Personne n'a encore pris cette issue.
- Langage dominant
- Lua
- Étoiles
- 4.4k
- Forks
- 442
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Aucun fichier d’implémentation ni test n’est nommé. Commencez par retracer comment les annotations Lua représentent les unions et comment le diagnostic param-type-mismatch les traite, puis comparez le comportement avec les exemples A & B, A | B et de fusion générique ; c’est terminé lorsque les annotations d’intersection évitent les faux avertissements démontrés, tandis que les unions conservent leurs vérifications actuelles.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- lua
- Domaine
- developer-experience, tooling
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100