LuaLS / LuaLS/lua-language-server

Feature Request: Capture table keys similar to capturing generic types

Ouverte
#2,058 1 commentaire 12 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

enhancement
Langage dominant
Lua
Étoiles
4.4k
Forks
442
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I like [TypeScript's `keyof` operator](https://www.typescriptlang.org/docs/handbook/2/keyof-types.html) that basically suggests that the value of a given string must match one of the properties/keys of a specified object.

In Lua, a type declared as `@type keyof queries` would mean it is a string whose value is a key found in the `queries` table.

This is useful because I can change just the value types if the keys of a table parameter were to be captured and specified as the target for a returned table type.

Here's an example of what I currently have to show the problem:

```lua
---@param queries table
---@return table
local function QueryMany(queries)
local results = {}

for key, query in pairs(queries) do
results[key] = Query(query) -- this function returns `QueryResult?`
end

return results
end
```

So, you can see that the `queries` argument is a table, and the keys should be exactly the same in the returned table, but only the values change.

Therefore, when I hover over the returned table in VS Code it doesn't know the keys in the table and only shows the key as being a string.

In the example above, the new annotation would look like this:

```lua
---@param queries table
---@return table
```

Currently, I need to create custom aliases or cast the returned tables manually when using the `QueryMany` example function to add the desired intelli-sense in VS Code. This is a problem because the function is designed to be dynamically reusable with arbitrary table arguments.

For context, this is how I would use the above function:
```lua
local results = QueryMany({
xSpacing = "frame.dimensions.xSpacing",
ySpacing = "frame.dimensions.ySpacing",
vDirection = "frame.behavior.vDirection",
hDirection = "frame.behavior.hDirection",
})
```
(**in summary:** _the table is transformed from a key-value pair of `string`/`string` to `string`/`QueryResult?` and there is no way of telling LuaLS this without losing the field/key names_)

You could also support `keyof` for standard aliases and types:

```lua
---@type keyof queries
local queryName = "" -- the value of queryName must match a field key found in the queries table

-- or as an alias:
---@alias QueryName keyof queries
```

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne nomme aucun fichier du dépôt, test ou point d’entrée. Commencez par localiser la capture des types génériques et la gestion des annotations existantes, puis suivez la manière dont les clés de table et les alias sont représentés. Le travail est terminé lorsque `keyof queries` conserve les clés de la table d’entrée pour les tables retournées et valide les valeurs autonomes ou les alias par rapport à ces clés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
lua
Domaine
devtools
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

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.