LuaLS / LuaLS/lua-language-server

Overload type narrowing based on number of arguments instead of argument-type

Open
#2,276 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

With the following code:
```
---@overload fun(param1: "test", param2: string)
---@overload fun(param1: "test2", param2: number)
---@overload fun(param1: "test3", param2: boolean, param3: number)
local function test(...)
end

test("test", "string"); ---expected: good, actual: good
test("test2", 1); ---expected: good, actual: good
test("test3", true, 1); ---expected: good, actual: good

test("test", 1); ---expected: bad, actual: good
test("test2", "string"); ---expected: bad, actual: good

test("test", true, 2) ---expected: bad, actual: bad
test("test3", "string"); ---expected: bad, actual: bad
test("test3", "string", 1); ---expected: bad, actual: bad
test("test3", true); ---expected: bad, actual: bad
```

I get the following errors:
Screenshot_1

With these overloads I would of expected the type to be equivalent to this:
```
{
param1: "test"
param2: string
} | {
param1: "test2"
param2: number
} | {
param1: "test3"
param2: boolean
param3: number
}
```
However, it is actually equivalent to:
```
{
param1: "test" | "test2"
param2: string | number
} |
{
param1: "test3"
param2: boolean
param3: number
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the overload example in the issue and trace the language server's type-checking path for overloaded function calls. The fix is complete when argument types narrow overloads as well as argument counts, accepting the three valid calls and rejecting the mismatched calls shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.