字面量类型

注解

字面量类型(String literal types)允许你指定字符串作为固定的代码提示,结合 @alias 特性可以起到类似“枚举”的效果

  • 示例

    ---@alias Handler fun(type: string, data: any):void
    
    ---@param event string | "'onClosed'" | "'onData'"
    ---@param handler Handler | "function(type, data) print(data) end"
    function addEventListener(event, handler)
    end
    
    ../_images/string_literal_types_1.png

    ../_images/string_literal_types_2.png

    注解

    建议使用 @alias 简化类型复杂度

    ---@alias Handler fun(type: string, data: any):void
    
    ---@alias IOEventEnum string | "'onClosed'" | "'onData'"
    
    ---@param event IOEventEnum
    ---@param handler Handler | "function(type, data) print(data) end"
    function addEventListener(event, handler)
    end
    
    ../_images/string_literal_types_3.png
Fork me on GitHub