Skip to main content

Overview

@generic adds type parameters to a function. GLuaLS uses them to carry types through helpers such as filter, map, and wrapper functions.

Syntax


Basic generic function


Multiple type parameters


Generic with constraint

Use : ConstraintType to restrict the types that T accepts:

Generic arrays


When to use generics

Generics work best for:
  • Functions that transform a value but keep its type
  • Functions that filter or map over a typed list
  • Functions that take a callback and return its result type
For simple cases, a union type or any can be easier to read.

String-template capture (GLuaLS)

Use backticks when a string argument names a class or type. Use this when a function takes a class name as text and returns that class type:
For collection-returning functions:
Rule:
  • T uses the normal Lua value type ("x" becomes string)
  • `T` uses the string text as a class or type name ("sent_npc" becomes sent_npc)
Common string-template forms:

Explicit generic call arguments

When inference is unclear, pass generics at the call site: