Skip to content

Reassigning nullable in if block #1664

@SalavatR

Description

@SalavatR

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

After cheking for null in IF block, variable should return not null.

Actual Behaviour

After cheking for null in IF block, variable returns nullable value, so it causes warning

solution: check in two if blocks or use --[[@as not_nullable_type]]

Reproduction steps

---@Class Mclass
local var1= {
---@type number?
var2 = nil,
}

---@param number
local function fun1(var2) end


if var1 and var1.var2 then
local var2= var1.var2
fun1(var2) --Cannot assign number? to parameter number.
end

Additional Notes

No response

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeat/type checkRelated to the type checking feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions