lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Using a union of string literals as a table key doesn't force use of the keys

Open kamoshi opened this issue 1 year ago • 3 comments

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Type Checking

Expected Behaviour

---@alias SomeUnion
--- | 'a'
--- | 'b'

---@type table<SomeUnion, number>
local a

should work roughly like

---@class (exact) SomeUnion
---@field a number
---@field b number
local a

Actual Behaviour

image

Reproduction steps

---@alias SomeUnion
--- | 'a'
--- | 'b'

---@type table<SomeUnion, number>
local a = {
  c = "should be invalid"
}

Additional Notes

No response

Log File

No response

kamoshi avatar Jan 23 '24 19:01 kamoshi

Here is how it would look like in TypeScript:

type SomeUnion = 'a' | 'b'
type SomeTable = Record<SomeUnion, number>

const a: SomeTable = {
}

const b: SomeTable = {
  a: 2,
  b: 3,
  c: 4,
}

image

kamoshi avatar Jan 23 '24 19:01 kamoshi

There's a lot of issues with the support for aliases/custom types. Seems to be an experimental feature that doesn't have full support.

As an example, there's no information in the documentation I linked that informs what the grammar for alias is incomplete. (For example, you can put some tokens like + and > after the | and it will work and change how the autodoc is formatted.

Would like first class support for this, though I suspect it is coming with the rework.

bavalpey avatar Feb 09 '24 02:02 bavalpey

see also #2610, #1861

mycroftjr avatar Jun 26 '24 23:06 mycroftjr