Skip to content

Overloading Class Constructors #2083

@firas-assaad

Description

@firas-assaad

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

In older versions (3.4), the following generated warnings for passing invalid values to the class construction function:

---@class Options
---@field name string

---@class MyClass
---@overload fun(x : Options) : MyClass
---@overload fun(x : string) : MyClass
local MyClass = {}

local w = MyClass(1) -- Cannot assign `integer` to parameter `string|Options`.
local z = MyClass { name = 1 } -- Cannot assign `integer` to `string`.

Actual Behaviour

In the current version, any value can be passed to the constructor without any warnings. I assume this is to support arbitrary __call metamethod, but in that case it's better to explicitly use the @opeartor annotation, and the extra warnings in the old version were very useful to detect invalid constructor parameters.

Reproduction steps

Try the example above.

Additional Notes

No response

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions