### Using t.optional for Type Composition in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md This example demonstrates the use of `t.optional` to create a type checker that accepts either a specified type or `nil`. It shows how this composite type checker behaves with a string, a nil value, and a value of an incorrect type. ```Lua local mightBeAString = t.optional(t.string) print(mightBeAString("Hello")) --> true print(mightBeAString()) --> true print(mightBeAString(1)) --> false, "(optional) string expected, got number" ``` -------------------------------- ### Creating Custom Type Checkers for OOP Integration in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md Provides an example of how to create a custom type checker function (`instanceOfClass`) that integrates seamlessly with an OOP framework. This allows the 't' library to validate whether a given value is an instance of a specific custom class, demonstrating extensibility. ```Lua local MyClass = {} MyClass.__index = MyClass function MyClass.new() local self = setmetatable({}, MyClass) -- setup instance return self end local function instanceOfClass(class) return function(value) local tableSuccess, tableErrMsg = t.table(value) if not tableSuccess then return false, tableErrMsg or "" -- pass error message for value not being a table end local mt = getmetatable(value) if not mt or mt.__index ~= class then return false, "bad member of class" -- custom error message end return true -- all checks passed end end local instanceOfMyClass = instanceOfClass(MyClass) local myObject = MyClass.new() print(instanceOfMyClass(myObject)) --> true ``` -------------------------------- ### Accessing Roblox Primitive Type Checkers in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md This example illustrates how to access and use built-in type checkers for Roblox-specific primitive types provided by the 't' module. It demonstrates that common Roblox types like Instance, CFrame, Color3, and Vector3 have corresponding type checkers available. ```Lua t.Instance t.CFrame t.Color3 t.Vector3 -- etc... ``` -------------------------------- ### Wrapping Functions with t.wrap using a Predefined Check in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md Illustrates how to use `t.wrap` to automatically validate function arguments. This example uses a previously defined `t.tuple` type check variable (`fooCheck`) to specify the expected argument types. ```Lua local fooCheck = t.tuple(t.string, t.number, t.optional(t.string)) local foo = t.wrap(function(a, b, c) -- function now assumes a, b, c are valid end, fooCheck) ``` -------------------------------- ### Define Basic Interface with t.interface Source: https://github.com/osyrisrblx/t/blob/master/README.md Demonstrates how to define a basic interface using `t.interface` with type checkers for fields. Shows how to apply the interface to a table and the resulting validation. ```Lua local IPlayer = t.interface({ Name = t.string, Score = t.number, }) local myPlayer = { Name = "TestPlayer", Score = 100 } print(IPlayer(myPlayer)) --> true print(IPlayer({})) --> false, "[interface] bad value for Name: string expected, got nil" ``` -------------------------------- ### Define Strict Interface with t.strictInterface Source: https://github.com/osyrisrblx/t/blob/master/README.md Shows how to use `t.strictInterface` to ensure that a table exactly matches the defined interface, disallowing any extra fields not specified in the definition. ```Lua local IPlayer = t.strictInterface({ Name = t.string, Score = t.number, }) local myPlayer1 = { Name = "TestPlayer", Score = 100 } local myPlayer2 = { Name = "TestPlayer", Score = 100, A = 1 } print(IPlayer(myPlayer1)) --> true print(IPlayer(myPlayer2)) --> false, "[interface] unexpected field 'A'" ``` -------------------------------- ### t.wrap API Reference Source: https://github.com/osyrisrblx/t/blob/master/README.md API documentation for the `t.wrap` function, which simplifies runtime type validation by automatically asserting argument types based on a provided check. It wraps a given callback function. ```APIDOC t.wrap(callback, argCheck) callback: The function to wrap. argCheck: The type checker to apply to the callback's arguments. ``` -------------------------------- ### Basic Runtime Type Checking with Assert in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md Demonstrates a common pattern for runtime type validation using `t.tuple` and `assert` to ensure function arguments conform to a predefined type signature. This approach explicitly calls `assert` with the result of the type check. ```Lua local fooCheck = t.tuple(t.string, t.number, t.optional(t.string)) local function foo(a, b, c) assert(fooCheck(a, b, c)) -- function now assumes a, b, c are valid end ``` -------------------------------- ### Define Nested Interface with t.interface Source: https://github.com/osyrisrblx/t/blob/master/README.md Illustrates how to create interfaces with nested interface definitions using `t.interface` to validate complex data structures. ```Lua local IPlayer = t.interface({ Name = t.string, Score = t.number, Inventory = t.interface({ Size = t.number }) }) local myPlayer = { Name = "TestPlayer", Score = 100, Inventory = { Size = 20 } } print(IPlayer(myPlayer)) --> true ``` -------------------------------- ### t Library Roblox Instance Type Checkers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents functions for validating Roblox Instances based on their ClassName, including exact matches, IsA comparisons, and checking child instances. ```APIDOC t.instanceOf(className: string, childTable?: table): description: Ensures the value is a Roblox Instance and its ClassName exactly matches 'className'. Optionally, if 'childTable' is provided, it will be automatically passed to t.children() for child validation. parameters: className: The exact ClassName string to match. childTable: (Optional) A table defining expected children, passed to t.children(). t.instanceIsA(className: string, childTable?: table): description: Ensures the value is a Roblox Instance and its ClassName matches 'className' via an IsA comparison (e.g., Instance:IsA(className)). Optionally, if 'childTable' is provided, it will be automatically passed to t.children() for child validation. parameters: className: The ClassName string to check against using IsA. childTable: (Optional) A table defining expected children, passed to t.children(). t.children(checkTable: table): description: Takes a table where keys are child names and values are functions to check the children against. Pass an instance tree into the function. Warning: If the tree contains more than one child of the same name, this function will always return false. parameters: checkTable: A table where keys are child names (string) and values are type checker functions (function) for those children. ``` -------------------------------- ### Basic Type Checking with t.tuple in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md This snippet demonstrates how to define a type check for function arguments using `t.tuple`. It shows how to specify required and optional types within a tuple and how the `assert` function can be used to enforce these types at runtime, catching type mismatches. ```Lua local t = require(path.to.t) local fooCheck = t.tuple(t.string, t.number, t.optional(t.string)) local function foo(a, b, c) assert(fooCheck(a, b, c)) -- you can now assume: -- a is a string -- b is a number -- c is either a string or nil end foo() --> Error: Bad tuple index #1: string expected, got nil foo("1", 2) foo("1", 2, "3") foo("1", 2, 3) --> Error: Bad tuple index #3: (optional) string expected, got number ``` -------------------------------- ### t Library String Type Checkers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents functions in the `t` library for validating string values, specifically matching patterns. ```APIDOC t.match(pattern: string): description: Checks if a value is a string and matches the provided 'pattern' using `string.match(value, pattern)`. parameters: pattern: The Lua pattern string to match against. ``` -------------------------------- ### Wrapping Functions with Inline Type Checks using t.wrap in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md Shows a more concise way to use `t.wrap` by defining the type check directly within the `t.wrap` call. This eliminates the need for a separate type variable, making the code more compact. ```Lua local foo = t.wrap(function(a, b, c) -- function now assumes a, b, c are valid end, t.tuple(t.string, t.number, t.optional(t.string))) ``` -------------------------------- ### t Module Meta Type Functions API Reference Source: https://github.com/osyrisrblx/t/blob/master/README.md This section provides an API reference for the meta type functions available in the 't' module. These functions allow for advanced type composition and validation logic, such as checking for any non-nil value, literal matches, key/value checks in tables, optional types, tuples, unions, and intersections. ```APIDOC t.any: description: Passes if value is non-nil. t.literal(...): description: Passes if value matches any given value exactly. parameters: - name: values type: any description: One or more values to match against. t.keyOf(keyTable): description: Returns a t.union of each key in the table as a t.literal. parameters: - name: keyTable type: table description: The table whose keys will be used to form the union of literals. t.valueOf(valueTable): description: Returns a t.union of each value in the table as a t.literal. parameters: - name: valueTable type: table description: The table whose values will be used to form the union of literals. t.optional(check): description: Passes if value is either nil or passes 'check'. parameters: - name: check type: function description: The type checker to apply if the value is not nil. t.tuple(...): description: Defines a tuple type. The arguments should be a list of type checkers. parameters: - name: checkers type: function[] description: A list of type checkers, one for each element in the tuple. t.union(...) (alias: t.some(...)): description: Defines a union type. At least one check must pass (i.e., 'a OR b OR c'). parameters: - name: checkers type: function[] description: A list of type checkers; if any pass, the union passes. t.intersection(...) (alias: t.every(...)): description: Defines an intersection type. All checks must pass (i.e., 'a AND b AND c'). parameters: - name: checkers type: function[] description: A list of type checkers; all must pass for the intersection to pass. t.keys(check): description: Matches a table's keys against 'check'. parameters: - name: check type: function description: The type checker to apply to each key in the table. t.values(check): description: Matches a table's values against 'check'. parameters: - name: check type: function description: The type checker to apply to each value in the table. t.map(keyCheck, valueCheck): description: Checks all of a table's keys against 'keyCheck' and all of a table's values against 'valueCheck'. parameters: - name: keyCheck type: function description: The type checker for table keys. - name: valueCheck type: function description: The type checker for table values. ``` -------------------------------- ### t.strict API Reference Source: https://github.com/osyrisrblx/t/blob/master/README.md API documentation for the `t.strict` function. It wraps an existing type checker, causing it to automatically run an `assert` on calls, thereby enforcing strict type adherence for the checked values. ```APIDOC t.strict(check) check: The type checker to make strict. ``` -------------------------------- ### t Library Array Type Checkers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents functions in the `t` library for validating Lua arrays, ensuring sequential integer keys and type-checked values. ```APIDOC t.array(check: function): description: Determines if a value is a table, all its keys are sequential integers, and all values in the table match the provided 'check' function. parameters: check: A type checker function (e.g., t.string, t.number) to apply to each element in the array. ``` -------------------------------- ### t Library Number Type Checkers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents various functions in the `t` library for validating numeric values, including checks for NaN, integers, positive/negative numbers, and range constraints (min/max, inclusive/exclusive). ```APIDOC t.nan: description: Determines if a value is NaN. Note: Other number checks will not pass for NaN values unless explicitly allowed with t.union(t.number, t.nan). t.integer: description: Checks if a value is a number and an integer. t.numberPositive: description: Checks if a value is a number and greater than 0. t.numberNegative: description: Checks if a value is a number and less than 0. t.numberMin(min: number): description: Checks if a value is a number and greater than or equal to 'min'. parameters: min: The minimum allowed numeric value (inclusive). t.numberMax(max: number): description: Checks if a value is a number and less than or equal to 'max'. parameters: max: The maximum allowed numeric value (inclusive). t.numberConstrained(min: number, max: number): description: Checks if a value is a number and falls within the inclusive range [min, max]. parameters: min: The minimum allowed numeric value (inclusive). max: The maximum allowed numeric value (inclusive). t.numberMinExclusive(min: number): description: Checks if a value is a number and strictly greater than 'min'. parameters: min: The minimum allowed numeric value (exclusive). t.numberMaxExclusive(max: number): description: Checks if a value is a number and strictly less than 'max'. parameters: max: The maximum allowed numeric value (exclusive). t.numberConstrainedExclusive(min: number, max: number): description: Checks if a value is a number and falls within the exclusive range (min, max). parameters: min: The minimum allowed numeric value (exclusive). max: The maximum allowed numeric value (exclusive). ``` -------------------------------- ### Checking Values Against Primitive Types in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md This snippet shows how to use the 't' module's primitive type checkers (e.g., `t.number`, `t.string`) to validate the type of a variable. It demonstrates both a successful type check returning true and a failed check returning false with an error message. ```Lua local x = 1 print(t.number(x)) --> true print(t.string(x)) --> false, "string expected, got number" ``` -------------------------------- ### Applying Strict Type Checking with t.strict in Lua Source: https://github.com/osyrisrblx/t/blob/master/README.md Demonstrates how to use `t.strict` to enforce type validation on function arguments. By wrapping the `t.tuple` check with `t.strict`, the `fooCheck` function itself performs the assertion when called. ```Lua local fooCheck = t.strict(t.tuple(t.string, t.number, t.optional(t.string))) local function foo(a, b, c) fooCheck(a, b, c) -- function now assumes a, b, c are valid end ``` -------------------------------- ### t Library Roblox Enum Type Checkers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents functions for validating Roblox Enum and EnumItem types, including checking if an EnumItem belongs to a specific Enum. ```APIDOC t.Enum: description: Ensures the value is a Roblox Enum (e.g., Enum.Material). t.EnumItem: description: Ensures the value is a Roblox EnumItem (e.g., Enum.Material.Plastic). t.enum(enum: Enum): description: Ensures the value is a Roblox EnumItem that belongs to the specified 'enum' type. parameters: enum: The Enum type (e.g., Enum.Material) to which the EnumItem must belong. ``` -------------------------------- ### t Library Interface Modifiers Source: https://github.com/osyrisrblx/t/blob/master/README.md Documents utility functions for modifying interface field behavior, such as making fields optional or allowing multiple types for a single field. ```APIDOC t.optional(check: function): description: Marks an interface field as optional. The field will be valid if it's absent or if its value matches the 'check' function. parameters: check: The type checker function for the optional field. t.union(...: function[]): description: Allows an interface field to match any of the provided type checker functions. parameters: ...: A variadic list of type checker functions. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.