Skip to content

@generic class incorrect type checking #1378

Closed as not planned
Closed as not planned
@carsakiller

Description

@carsakiller

@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 #1362 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancementNew feature or requestfeat/genericRelated to generic emulation featurefeat/type checkRelated to the type checking feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions