Skip to content

Use type info from __newindex annotations in metatable instead of table's #3020

@bew

Description

@bew

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Type Checking

Expected Behaviour

Accept tbl.foo = bar where tbl's keys are typed to a different type than __newindex's value type.

Actual Behaviour

Get error because bar's type is different from tbl's keys type.

Reproduction steps

With this initial code:

---@class PlugTagSpec ---@field name string ---@field desc string ---@type {[string]: PlugTagSpec} local predefined_tags = setmetatable({}, { ---@param name string ---@param spec {name?: string, desc: string} __newindex = function(self, name, spec) spec.name = name -- add name in spec rawset(self, name, spec) end, })

Use it with:

predefined_tags.foo = { desc = "bar" }

👉 Get error:

Missing required fields in type `PlugTagSpec`: `name` 

.. even though __newindex explicitely does not require the value to be a full spec but only its description (desc).

Additional Notes

Related: #2941

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions