LuaLS / LuaLS/lua-language-server

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

Open
#2,476 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feat/LuaCats Annotations feat/type check
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by comparing the requested @guard annotation and isString example with the language server's existing type-narrowing behavior. Done means user-defined guards narrow x to string in the true branch and to number in the false branch.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.