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

overload type narrowing for returns #2933

Open
Bilal2453 opened this issue Nov 3, 2024 · 0 comments
Open

overload type narrowing for returns #2933

Bilal2453 opened this issue Nov 3, 2024 · 0 comments

Comments

@Bilal2453
Copy link
Contributor

Bilal2453 commented Nov 3, 2024

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Linux

What is the issue affecting?

Type Checking, Completion

Expected Behaviour

I am not sure this is a duplicate issue considering how common it probably is, but just to make sure.
Possibly a duplicate of #1583.
This is similar to #1146, but with returns instead of params.

It is a common pattern in Lua that on errors you might instead return a nil value to imply failure, along side an error message.

---@return table
---@overload fun(): nil, string
local function fetchX() end

local resource, fail = fetchX()
if not resource then
  print(fail:sub(1, 3)) -- fail type here is expected to be "string"
end

Actual Behaviour

You get a warning on fail that it might possibly be nil. Because its type is string|nil.

---@return table
---@overload fun(): nil, string
local function fetchX() end

local resource, fail = fetchX()
if not resource then
  print(fail:sub(1, 3) )-- fail type here is actually "string|nil"
end

Reproduction steps

  1. Copy the setup code
  2. follow along with the comments

Additional Notes

I believe narrowing down the types in this case is still possible? It seems to me like #1583 was discussing the possibility of adding tuples, but shouldn't this be doable even without tuples support?

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