LuaLS / LuaLS/lua-language-server

[Feature Request] Intersection types

Aberta
#3,152 3 comentários 4 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Linguagem predominante
Lua
Estrelas
4.4k
Forks
442
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Nenhum arquivo de implementação ou teste é nomeado. Comece rastreando como as anotações Lua representam uniões e como o diagnóstico param-type-mismatch lida com elas; em seguida, compare o comportamento com os exemplos A & B, A | B e de merge genérico; considera-se concluído quando as anotações de interseção evitarem os falsos avisos demonstrados, enquanto as uniões mantiverem suas verificações atuais.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
lua
Domínio
developer-experience, tooling
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.