LuaLS / LuaLS/lua-language-server

Question mark optional syntax not supported in fun() declarations

Open
#2,385 1 comment 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

Optional value to actually be optional

### Actual Behaviour

It's not optional.
![image](https://github.com/LuaLS/lua-language-server/assets/19525688/01552e48-a1b3-4e7b-913e-9d3f86d66e30)
This code block should show as Human? and throw warnings for not checking nil. If I manually annotate with `---@param man Human?` this works as expected.

The class in question:
```lua
---@class Command
---@field info string What the command does.
---@field usage string? How to use the command.
---@field alias string[]? Aliases of the command.
---@field canCall (fun(player: Player): boolean)? Function which checks whether a player can call this command.
---@field call fun(player: Player, human: Human?, args: string[]) Calls the command.
---@field autoComplete fun(args: string[])? Function which manipulates arguments when pressing tab in the terminal.
---@field cooldownTime number? How many seconds a player has to wait before using the command again.
```

I have also tried with human?: Human and human?: Human? but neither way works.

### Reproduction steps

1. Create class with a field with a short form function declaration with an optional value parameter
2. Attempt to create object of class with that field,
3. Parameter correctly types, except as not optional.

### 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 class-field example in the Visual Studio Code extension, focusing on the short-form function declaration with a nullable Human parameter. Compare it with the manually annotated `---@param man Human?` case; done means the parameter is inferred as optional and nil-check warnings appear as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, vscode
Domain
devtools
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.