You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect type on string-key fields when combined with [string] #3365
since heat-visualizer-updates-per-surface-per-tick contains special character -
when you choose the auto completion for this field, the server will wrongly complete it as settings.global[heat-visualizer-updates-per-surface-per-tick] without double quotes 😕
I think this is another bug
=> server should adds double quote if key name contains special character / is a reserve word
analysis
I have fixed a similar issue before in PR #3224, but further testing found out that this issue is a bit different 😕
seems that for ---@field const_name type def, the server will treat them has having a concrete field key
(i duno how to express this concept better 🙈 i am just an amateur contributor and dont know much about how language server works)
=> and always narrow to the defined type
but for a ---@field [expr] type, server will merge all the types if the current value can be casted to the expr type.
This syntax is not for defining a single concrete field key type, as you can write ---@field ["a"|"b"|"c"] string
i should probably note that the specific-value strings will vary for each user who generates these depending on what other related bits they have installed locally
localfunctionparseIndexField(parent)
ifnotcheckToken('symbol', '[', 1) thenreturnnilendnextToken()
localfield=parseType(parent)
nextSymbolOrError']'iffieldand#field.types==1and (
field.types[1].type=='doc.type.string'orfield.types[1].type=='doc.type.integer'
)
then-- for field name having special characters, eg:-- ---@field ["field-key"] string-- ---@field [1] boolean-- return them as a single named fieldreturnfield.types[1]
endreturnfieldend
after parsing an indexed field, if the types defined within only contains exactly 1 string or integer
=> just return this field type object directly
this seems let the server "extract" the actual field literal when tracing the field type, and skips the original fallback index infer logic 🤔
As noted above, this only works for ---@field ["key-a"] type but not for ---@field["key-a"|"key-b"] type.
Also I haven't tested if it would cause other problems.
You may want to try this patch and open a PR after adding more test cases 👀
As noted above, this only works for ---@field ["key-a"] type but not for ---@field["key-a"|"key-b"] type.
in general it seems like it should prefer the narrowest matching field def to allow for the second example but the first one would be sufficient for my immediate needs
Activity
tomlau10 commentedon Mar 5, 2026
workaround
I tested a bit and found that if you write the annotation as
then the type narrowing will work.
But this has a side effect:
heat-visualizer-updates-per-surface-per-tickcontains special character-settings.global[heat-visualizer-updates-per-surface-per-tick]without double quotes 😕=> server should adds double quote if key name contains special character / is a reserve word
analysis
I have fixed a similar issue before in PR #3224, but further testing found out that this issue is a bit different 😕
---@field const_name typedef, the server will treat them has having a concrete field key(i duno how to express this concept better 🙈 i am just an amateur contributor and dont know much about how language server works)
=> and always narrow to the defined type
lua-language-server/script/vm/compiler.lua
Lines 527 to 553 in d965edc
---@field [expr] type, server will merge all the types if the current value can be casted to the expr type.This syntax is not for defining a single concrete field key type, as you can write
---@field ["a"|"b"|"c"] stringlua-language-server/script/vm/compiler.lua
Lines 624 to 675 in d965edc
Unfortunately I have no idea how this can be fixed / let alone what's the best way to fix it 😕
---@field ["a"|"b"|"c"] string=> there are multiple string literals 😇justarandomgeek commentedon Mar 5, 2026
i should probably note that the specific-value strings will vary for each user who generates these depending on what other related bits they have installed locally
tomlau10 commentedon Mar 6, 2026
By using this approach, I seem to make it work:
local function parseIndexFieldinscript/parser/luadoc.lualua-language-server/script/parser/luadoc.lua
Lines 292 to 300 in d965edc
=> just return this field type object directly
---@field ["key-a"] typebut not for---@field["key-a"|"key-b"] type.Also I haven't tested if it would cause other problems.
You may want to try this patch and open a PR after adding more test cases 👀
justarandomgeek commentedon Mar 6, 2026
in general it seems like it should prefer the narrowest matching field def to allow for the second example but the first one would be sufficient for my immediate needs