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

`@generic` misbehaving when used with a class

Open carsakiller opened this issue 3 years ago • 0 comments

@generic seems to be behaving weirdly when being used in a class.

---@class Array<T>: { [integer]: T }

---@type Array<string>
local arr = {}

-- correctly warns that I am assigning a boolean to a string
arr[1] = false

-- no warning despite indexing with a string instead of an integer
arr["foo"] = "bar"

-- correctly does not warn as I am following the type correctly
arr[3] = "Correct"

Behaves even weirder for a dictionary:

---@class Dictionary<T>: { [string]: T }

---@type Dictionary<boolean>
local dict = {}

-- no warning despite indexing with a number
dict[1] = "incorrect"

-- no warning despite assigning a string
dict["foo"] = "bar?"

-- correctly does not warn as I am following the type correctly
dict["correct"] = true

The same issues exist when indexing with . like dict.foo = "bar?"

Originally posted by @carsakiller in https://github.com/sumneko/lua-language-server/discussions/1362#discussioncomment-3210912

carsakiller avatar Jul 23 '22 14:07 carsakiller