LuaLS / LuaLS/lua-language-server
Nested Table Type Narrowing
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
It would be super nice to be able to get field suggestions when narrowing an indice of a nested table.
Currently this:
---@class TestClass
---@field test_group_a TestGroupA
---@field test_group_b TestGroupB
---@class TestGroupA : TestGroupCommon
---@field test_field_1 string
---@field test_field_2 string
---@class TestGroupB : TestGroupCommon
---@field test_field_1 string
---@field test_field_3 string
---@class TestGroupCommon
---@field test_field_common string
---@type TestClass
local test_class
---@type 'test_group_a' | 'test_group_b'
local test_group
local test1 = test_class[test_group].test_field_1
---------------------------------------
---@alias TestTable {test_group_a: {test_field_1: string, test_field_2: string}, test_group_b: {test_field_1: string, test_field_3: string}}
---@type TestTable
local test_table
---@type "test_group_a" | "test_group_b"
local test_table_group
local test2 = test_table[test_table_group].test_field_1
The inferred type for test1 and test2 are both unknown. It would be nice if for test1 and test2 the inferred type would be string (as test_field_1 is a string for both, if test_field_1 and test_field_2 differed the inferred the type should be test_field_1_type | test_field_2_type). It would also be nice if on local test1 = test_class[test_group]. there were field suggestions for test_field_1 and test_field_common which are on both TestGroupA and TestGroupB.
Note: to add this for classes would also require doing: https://github.com/LuaLS/lua-language-server/issues/2217
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two Lua examples in the issue and inspect the language-server paths that handle indexed access, type narrowing, and field completion. Done means both examples infer string for test_field_1 and offer the fields shared by the possible nested table types, while accounting for the related class-narrowing issue 2217.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100