Skip to content

Even if the self parameter is defined with another type, it is automatically defined in the body of the function as the type of the object. #3120

Description

@MillhioreBT

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Type Checking

Expected Behaviour

test.lua - No error.

---@class Position
---@field x number
---@field y number
---@field z number
Position = {}

---@class Creature
---@field teleportTo fun(self:Creature, position:Position):nil
Creature = {}

---@class Zone
---@field getName fun(self:Zone):string
---@overload fun(): Zone
Zone = {}

---@class Event
---@field onCreatureEnter fun(self:Zone, creature:Creature):nil
---@field onCreatureLeave fun(self:Zone, creature:Creature):nil
---@field register fun(self:Event):nil
---@overload fun(): Event
Event = {}

local zone = Zone()

local zoneEvent = Event()

function zoneEvent:onCreatureEnter(creature)
	if self:getName() == zone:getName() then
		creature:teleportTo(Position(0, 0, 0))
	end
end

zoneEvent:register()

Actual Behaviour

test.lua - Found error.

---@class Position
---@field x number
---@field y number
---@field z number
Position = {}

---@class Creature
---@field teleportTo fun(self:Creature, position:Position):nil
Creature = {}

---@class Zone
---@field getName fun(self:Zone):string
---@overload fun(): Zone
Zone = {}

---@class Event
---@field onCreatureEnter fun(self:Zone, creature:Creature):nil
---@field onCreatureLeave fun(self:Zone, creature:Creature):nil
---@field register fun(self:Event):nil
---@overload fun(): Event
Event = {}

local zone = Zone()

local zoneEvent = Event()

function zoneEvent:onCreatureEnter(creature)
	if self:getName() == zone:getName() then
		creature:teleportTo(Position(0, 0, 0))
	end
end

zoneEvent:register()

Images example

Image

Image

Reproduction steps

Just copy the code and test it in any Lua file.
self should be of type Zone, as that's how it's defined in the meta file.

Additional Notes

No response

Log File

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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