Skip to content

[fix/type-checking] ternary operator incorrect type infer #3154

Closed as duplicate

Description

@AgatZan

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Windows

What is the issue affecting?

Type Checking, Annotations

Expected Behaviour

TO BE

code 1

---@type string|string[]
local x = ""

-- TO BE xs: string[] 
-- AS IS xs: string[] | string
local xs = type(a) == "string" and {x} or x

if type(x) == "string" then
  xs = {x}
else
  xs = a
end
-- but xs:string[]

Actual Behaviour

AS IS

Reproduction steps

  1. Create file temp.lua
  2. Append code 1
  3. See type of xs

Additional Notes

No response

Log File

Log file

Activity

  1. changed the title [-]ternary operator incorrect type infer[/-] [+][fix/type-checking] ternary operator incorrect type infer[/+] on Apr 13, 2025
  2. tomlau10 commented on Apr 14, 2025

    @tomlau10
    Contributor

    I believe this is a duplicate of #2233, where the type narrow is not working correctly in ternary operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions