LuaLS / LuaLS/lua-language-server

Template Annotation

Open
#2,220 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

I love the new `missing-fields` diagnostic! It is super helpful to guarantee safety especially for function arguments (though its usefullness in my repo has stagnated slightly due to https://github.com/LuaLS/lua-language-server/issues/2214)

However, it does come into conflict with something I do in a few places where I specifically set a type annotation for the autocomplete where the data in the table is initialized later in a meta_table (or the data is only every accessible through the meta_table).

```lua
---@type WeaponsUnitData
Weapons.unit = {}

setmetatable(Weapons.unit, Metatable.unit_metatable())
```

With the new `missing-fields` diagnostic I get a warning here, that Weapons.unit doesn't contain all of the fields on WeaponsUnitData. I was wondering with the `missing-fields` diagnostic we could get a `---@template` annotation which could describe the shape of a table without having to define all of the fields on it. (As a cleaner alternative to having to do `---@diagnostic disable-next-line: missing-fields` in a bunch of places). I am sure other people were also using tables in other weird ways before as well, this would allow an easy transition for them.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing `missing-fields` diagnostic and the annotation handling involved in `---@type`. Determine how a `---@template` annotation could describe a table shape without requiring every field, then verify that the example using `Weapons.unit` no longer produces the warning while normal missing-field checks remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.