LuaLS / LuaLS/lua-language-server

Type Narrow Exact Strings

Open
#2,219 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Another feature suggestion 😅

It would be useful to be able to type-narrow exact strings.

For example:

---@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 "test_group_a" | "test_group_b"
local test

if test == "test_group_a" then
	---@type TestTable
	local test_table
	test1 = test_table[test].test_field_1
end

When we do if test == "test_group_a" then it would be nice if the type of test narrowed to test_group_a (which can then be used to successfully index test_table. As it is now there is no type narrowing so the inferred type of test1 is unknown when it should be string.

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

The issue's Lua example is the only entry point provided; use it to reproduce the current types of test and test1. Done means an equality check against "test_group_a" narrows test enough to index TestTable and infer test1 as string, including coverage for the shown case.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
compilers, 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.