LuaLS / LuaLS/lua-language-server

Type narrowing for unioned classes

Offen
#2,109 2 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

enhancement
Vorherrschende Sprache
Lua
Sterne
4.4k
Forks
442
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

image

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das annotierte Foo/Bar Prototypes[]-Beispiel aus diesem Issue zu reproduzieren und das Union-Typ-Narrowing sowie das Verhalten der Vervollständigung von Tabellenliteralen im Language Server zu untersuchen. Als abgeschlossen gilt die Aufgabe, wenn eine anhand des type-Felds eingegrenzte diskriminierte Union bei der Vervollständigung nur die Felder der ausgewählten Klasse anzeigt und gleichzeitig eine gültige Vervollständigung für andere Union-Mitglieder erhalten bleibt.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.