LuaLS / LuaLS/lua-language-server
Support discriminated unions / type narrowing
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
In TypeScript this is supported by "narrowing"
Consider this example:
---@alias ChatAction { type: '"chat"', targetName: string, msg: string }
---@alias NotifyAction { type: '"notify"', msg: string, colour: number }
---@alias ActionType ChatAction | NotifyAction
---@param action ActionType
local function example(action)
-- action is any action (properties 'type', 'msg')
if (action.type == 'chat') then
-- action is ChatAction (properties 'type', 'msg', 'targetName')
end
if (action.type == 'notify') then
-- action is NotifyAction (properties 'type', 'msg', 'colour')
end
end
For a union type such as 'ActionType' in the example, the only defined properties should be those common to all types. After narrowing (through some sort of type check, in this case a type field) the additional properties in the narrowed subset are also available.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the issue's ActionType example and read the linked TypeScript narrowing documentation to define the expected behavior. Done means union values expose only shared properties before a type check, while checks on the type field expose the properties of the matching action variant.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, typescript
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100