LuaLS / LuaLS/lua-language-server
Type narrowing for unioned classes
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 have a large number of classes that look something like this:
---@class Foo
---@field type "foo"
---@field name string
---@field property string
-- more unique fields
---@class Bar
---@field type "bar"
---@field name string
---@field stuff string
-- more unique fields
-- more classes
I have a function that takes an array of any of these classes as a parameter, so naturally I've unioned all of the classes.
---@alias Prototypes
---|Foo
---|Bar
-- all of the other classes
---@param prototypes Prototypes[]
local function extend(prototypes)
-- stuff and things
end
However, when I try to make prototypes a table literal, I get autocomplete for the other classes because the union operator behaves like intersection instead of union.
Is there any way to get it to show only the fields of the class indicated by the type field? I can't seem to find way to do a proper union of types.
@type seems to only work on variables, and even if it did work on table literals, it would be cumbersome to write out the type for every entry in the array.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’esempio annotato Foo/Bar Prototypes[] di questa issue e analizza il comportamento del restringimento dei tipi unione e del completamento dei letterali di tabella del server di linguaggio. Il lavoro è completo quando un’unione discriminata ristretta tramite il campo type presenta nel completamento solo i campi della classe selezionata, preservando al contempo un completamento valido per gli altri membri dell’unione.
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