Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MillhioreBT opened this issue Mar 16, 2025 · 0 comments

Comments

@MillhioreBT
Copy link

MillhioreBT commented Mar 16, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant