LuaLS / LuaLS/lua-language-server
Support discriminated unions / type narrowing
未关闭
还没有人认领这个 Issue。
enhancement
- 主要语言
- Lua
- 星标
- 4.4k
- 派生
- 442
- PR 合并指标
- 30 天内没有已合并 PR
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 的 ActionType 示例开始,并阅读链接的 TypeScript 类型收窄文档,以定义预期行为。完成的标准是:在进行类型检查之前,联合值只暴露共享属性;而对 type 字段进行检查时,则暴露匹配的 action variant 的属性。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- lua, typescript
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100