Skip to content

Type inference not working for @field and @type function declarations in method overrides #3367

@ChouUn

Description

@ChouUn

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Linux

What is the issue affecting?

Type Inference

Expected Behaviour

When a child class overrides a parent class method that was declared using @field or @type annotations, the parameter types should be correctly inferred from the parent class definition.

---@class Buff
local mt = {}
---@type (fun(self: Buff, target: Buff): boolean)?
mt.on_cover = nil

---@class Buff.CommandAura : Buff
local tpl = {}
function tpl:on_cover(target)
    -- Expected: target type should be Buff
    return self.level > target.level
end

Actual Behaviour

The parameter types are inferred as any instead of the types defined in the parent class.

---@class Buff
local mt = {}
---@type (fun(self: Buff, target: Buff): boolean)?
mt.on_cover = nil

---@class Buff.CommandAura : Buff
local tpl = {}
function tpl:on_cover(target)
    -- Actual: target type is any ❌
    return self.level > target.level
end

This also affects @field declarations:

---@class Animal
---@field can_eat (fun(self: Animal, other: Animal): boolean)?
local base = {}

---@class Dog : Animal
local dog = {}
function dog:can_eat(other)
    -- Actual: other type is any ❌
    return true
end

Reproduction steps

  1. Define a parent class with a method using @type or @field annotation
  2. Create a child class that inherits from the parent
  3. Override the method in the child class
  4. Check the parameter types - they will be any instead of the parent's types

Additional Notes

This issue is related to #2569, but specifically focuses on field-style function declarations (@field and @type) rather than method declarations (function Class:method()).

PR #2859 successfully implemented type inference for method declaration style overrides, but did not cover field-style declarations.

Root cause: In script/vm/compiler.lua, the compileFunctionParam function only checks for 'doc.type.function' and 'function' node types, but @field nodes have type 'doc.field' with the actual function type stored in the extends property.

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