lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Error returning derived classes

Open MillhioreBT opened this issue 1 year ago • 0 comments

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?

Annotations

Expected Behaviour

---@class Thing
---@field getCreature fun(self:Thing):Creature?
---@field getPlayer fun(self:Thing):Player?
---@field getMonster fun(self:Thing):Monster?
---@field getNpc fun(self:Thing):Npc?

---@class Creature:Thing
---@field getCreature fun(self:Creature):Creature
---@field getPlayer fun(self:Creature):Player?
---@field getMonster fun(self:Creature):Monster?

---@class Player:Creature
---@field getPlayer fun(self:Player):Player
---@field getMonster fun(self:Player):nil
---@field getNpc fun(self:Player):nil

---@class Monster:Creature
---@field getPlayer fun(self:Monster):nil
---@field getMonster fun(self:Monster):Monster
---@field getNpc fun(self:Monster):nil

function Creature:getCreature() return self end
function Creature:getPlayer() return self:isPlayer() and self or nil end
function Creature:getMonster() return self:isMonster() and self or nil end
function Creature:getNpc() return self:isNpc() and self or nil end

Actual Behaviour

The current behavior is that the methods: getMonster, getNpc throw the following error:

Annotations specify that return value #1 has a type of `Monster?`, returning value of type `Creature|nil` here instead.
- `Creature` cannot match `Monster?`
- Type `Creature` cannot match `nil`
- Type `Thing` cannot match `nil`
- Type `Creature` cannot match `Monster`
- Type `Thing` cannot match `Monster`Lua Diagnostics.(return-type-mismatch)

The error does not occur with the Player class which is also derived from the Creature class

Reproduction steps

  1. Use the code of the expected behavior

Additional Notes

No response

Log File

No response

MillhioreBT avatar Apr 02 '24 05:04 MillhioreBT