LuaLS / LuaLS/lua-language-server

Disallow ? on values

Open
#2,814 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### 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

`?` is not a type, and should only be used when marking fields as optional. If nil is explicitly allowed as a value, `| nil` should be used. This is in line with TypeScript.

### Actual Behaviour

`?` does not add `| nil` to the value type, and the second option should be disallowed.
![image](https://github.com/user-attachments/assets/7a4cd168-6590-4368-acfb-b13b2af9a9ea)

### Reproduction steps

```lua
---@class Foo
---@field foo? string
---@field bar string?
---@field baz string | nil
local test = {};

function Foo()
return test.foo, test.bar, test.baz
end;
```

### Additional Notes

_No response_

### Log File

_No response_

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

Start by reproducing the type-checking behavior in the Visual Studio Code Extension (sumneko.lua) using the Lua snippet in the issue. Done means `string?` is disallowed, `foo? string` remains the optional-field form, and `string | nil` explicitly permits nil.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.