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

Wrong parameter type when type() is used #1902

Closed
firas-assaad opened this issue Feb 11, 2023 · 1 comment
Closed

Wrong parameter type when type() is used #1902

firas-assaad opened this issue Feb 11, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@firas-assaad
Copy link
Contributor

firas-assaad commented Feb 11, 2023

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

Consider the following simplified example:

---@param p integer|string
local function get_val(p)
    local is_number = type(p) == 'number'
    return is_number and p or p
end

get_val('hi')

I expect the type of p to be integer|string and the call get_val('hi') to work without any warnings.

Actual Behaviour

I get a param-type-mismatch problem in the call get_val('hi'):

Cannot assign string to parameter integer.\n- string cannot match integer\n- Type string cannot match integer

Hovering over p in the function definition function get_val(p) I see the type as integer.

image

This happens when type() is assigned to a local variable. If I remove the local variable and change the function body to return type(p) == 'number' and p or p then everything works as expected.

Reproduction steps

Try the code above in VS Code with the latest version of the extension. Let me know if you need more information or logs.

Additional Notes

Probably a more general case of #1841

Log File

No response

@firas-assaad
Copy link
Contributor Author

Another example:

---@param p? string|integer
local function fun(p)
    local x = p
    local check = type(x) == 'string' or type(x) == 'number'
    return check
end

image

@sumneko sumneko added the bug Something isn't working label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants