Skip to content

[Feature request] User defined type guards like TypeScript's one #2476

Open

Description

@uga-rosa

As a simple example, I would like to annotate the following @guard x string. This is in effect returning a boolean, which narrows down the type of the argument x.

---@param x unknown
---@guard x string
local function isString(x)
  return type(x) == "string"
end

---@param x string | number
local function main(x)
  if isString(x) then
    -- x is string
    print(x)
  else
    -- x is number
    print(x)
  end
end

Activity

  1. firas-assaad commented on Jan 12, 2024

    @firas-assaad
    Contributor

    I'd like this feature as well. It's been proposed a couple of times in the past, but it's apparently hard to implement: #1030 (comment)

  2. itsDesignFlaw commented on Jan 28, 2026

    @itsDesignFlaw

    2 years later, is there any chance of getting this?

  3. CppCXY commented on Jan 28, 2026

    @CppCXY
    Member

    emmyluals support this like:

    Image
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

    enhancementNew feature or requestfeat/LuaCats AnnotationsRelated to Lua Language Server Annotations (LuaCats)feat/type checkRelated to the type checking feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions