-
-
Notifications
You must be signed in to change notification settings - Fork 351
:
are not typed checked properly
#2842
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
Comments
I think in the reproduction code snippet, that should be |
Well, ---@param a number|string
function Foo.Bar(a)
return a:char()
end This will result in a warning. |
This is a bit more "verbose", but it is what I prefer. ---@param a number|string
function Foo.Bar(a)
return (type(a) ~= "string" and tostring(a) or a):lower()
end |
Whether or not string.lower() accepts a number or not should not be relevant here. |
It is valid here as you are giving LuaLS the option of While LuaLS helps in a lot of ways, this is one of those situations that it does not. If your function expects a certain type of variable, you should be doing runtime type checking (
|
Um... I think this statement contradicts with the
You are right that luals cannot detect all the runtime errors, and we all know that. And that's the reason why issues exist: they are used to track these edge cases, letting maintainers know they exist, and hoping that they will be fixed by maintainers or contributors 😄 . The edge case described in this issueWhen luals tries to link a Furthermore, I know there is a config |
It is valid here as you are giving LuaLS the option of a being a string or a number. As such, it will not warn you since as one of the types you provided is valid (string) and a valid parameter for the function being called. |
yeah~ Don't know why I overlooked just now 😕 and you are correct on that both ---@number
local n
local s = string.char(n) --< this doesn't throw error?
|
---@type number
local n
local s = string.char(n) --< this doesn't throw error? ? |
numbers and integers are inter-changable assuming you have the setting toggled. |
Ah~ I see that Now with that, how come
By your a valid parameter for the function being called argument, luals should not throw error? |
Here is an example snippet showing that LuaLS doesn't work this way: 👀 ---@class A
local A
function A:test() end
---@class B
local B
---@type A|B
local c
c:test() -- param-type-mismatch: Cannot assign `A|B` to parameter `A`. - `B` cannot match `A` - Type `B` cannot match `A`
(off topic) ---@class A
local A = {}
function A:test() end
---@class B
local B = {}
---@type A|B
local c
c:test() -- no warnings no matter `type.weakUnionCheck = true/false`
|
Hey sorry, missed the replies Not sure I fully follow. So going back to the basics Let's take the example of ---@param b string
local function Baz(b)
end
---@param a number|string
function Foo.Bar(a)
if type(a) == 'string' then
return Baz(a) -- 100% fine
end
return Baz(a) -- Cannot assign `string|number` to parameter `string`. - `number` cannot match `string` - Type `number` cannot match `string` Lua Diagnostics.(param-type-mismatch)
end It requires that all types that haven't been narrowed are allowed ---@param a number|string
function Foo.Bar(a)
local c
c = string.lower(a) -- OK, string.lower() handles numbers and strings
c = number.lower(a) -- Undefined global `number`
c = integer.lower(a) -- Undefined global `integer`
c = a.lower(a) -- Does not error, but should. As a number/integer is not a table (unlike a string)
c = a:lower() -- Does not error, but should.
end
|
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
Reproduction steps
Additional Notes
#2708 (comment)
Log File
No response
The text was updated successfully, but these errors were encountered: