From 73fb63047e498828ad9edb238b1c36d8f00a8173 Mon Sep 17 00:00:00 2001 From: xiangnan <280145668@qq.com> Date: Fri, 17 Jul 2026 20:56:19 +0800 Subject: [PATCH] docs(wiki): document generic classes in @generic section The @generic section predates v3.17.0 and did not mention that classes can declare type parameters beyond the index-signature examples. Add: - a sentence in the intro listing the v3.17.0 additions (generic class inheritance with type arguments, generic parameters in @overload, inline fun function types in @field/@type) - an example of a generic class with fields and methods, including inheritance from an instantiated generic class, with a note on when type parameters are substituted - an example of the inline fun generic function type All examples verified against lua-language-server 3.18.2 using --check. Co-Authored-By: Claude Fable 5 --- src/content/wiki/annotations.mdx | 48 +++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/content/wiki/annotations.mdx b/src/content/wiki/annotations.mdx index 4d55632..07381bb 100644 --- a/src/content/wiki/annotations.mdx +++ b/src/content/wiki/annotations.mdx @@ -647,7 +647,7 @@ local Numbers = {} ### @generic -Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (`) will capture the string value from the argument and infer the named class/type as the generic type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861). +Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (`) will capture the string value from the argument and infer the named class/type as the generic type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861). Since `v3.17.0`, classes can declare type parameters (e.g. `Box`), classes can inherit from an instantiated generic class (e.g. `IntegerBox: Box`), generic type parameters work in [`@overload`](#overload) annotations, and `fun(...)` inline generic function types are supported in [`@field`](#field) and [`@type`](#type) annotations. **Syntax** @@ -733,6 +733,52 @@ dict["foo"] = "bar?" dict["correct"] = true ``` + + +Generic Class with Fields and Methods + +```lua +---@class Box +---@field value T +---@field get fun(self: Box): T + +---@type Box +local box + +-- `v` is an integer here +local v = box.value + +-- `got` is an integer here +local got = box:get() + +---A class can also inherit from an instantiated generic class (v3.17.0+) +---@class IntegerBox: Box + +---@type IntegerBox +local ibox + +-- `v2` and `got2` are integers here as well +local v2 = ibox.value +local got2 = ibox:get() +``` + +Note: for the type parameter to be substituted through inheritance, members must be declared as `---@field` entries in the class annotation block. A method declared separately (e.g. `---@return T` above `function Box:m() end`) only has `T` substituted when the receiver's type is the instantiated generic class itself (e.g. a value of type `Box`), not when the method is reached through an inheriting class like `IntegerBox`. + + + +Inline Generic Function Type (v3.17.0+) + +```lua +---@class Utils +---@field identity fun(value: T): T + +---@type Utils +local utils + +-- `s` is a string here +local s = utils.identity("hello") +``` + ### @meta