Typer
Strict typechecking in runtime implementation for Roblox.
Functions
Static
Typer.Static(CallbackFunction: (any?) → any?--
The wrapped function
) → StaticFunctionWraps a function so that all parameters and return values are matched to the correct types.
local ExampleStaticFunction = Typer.Static(function(Foo : number, Bar : string)
return `{Foo} and {Bar}`, {Foo = Foo, Bar = Bar}
end).WithParameterTypes("number", "string").WithReturnTypes("string", "table").Asserted()
local Value0, Value1 = ExampleStaticFunction(10, "Hello!") -- Does not error
local Value2, Value3 = ExampleStaticFunction({}, true) -- Errors
Cast
Typer.Cast(Value: any--
The cast value
) → (...: string): any--
The casting method
Checks if a value is of a certain type, allowing multiple types
local MyValue = "Hello, world!"
Typer.Cast(MyValue)("number", "string") -- No error
Typer.Cast(MyValue)("table") -- Error
CastEq
Typer.CastEq(Value0: any,--
The first value for comparison
Value1: any--
The second value for comparison
) → boolean--
Whether the values are the same
Checks if two values are of the same type, and returns a boolean based on whether they are the same value as well.
local Value0 = Typer.CastEq(1, 2) -- No error
print(Value0) -- Prints "false"
local Value1 = Typer.CastEq("Foo", "Foo") -- No error
print(Value1) -- Prints "true"
local Value2 = Typer.CastEq(1, "Foo") -- Error
TupleAssertion
Typer.TupleAssertion(...: string--
The inputted types, in order
) → (...any)--
The function used to check arguments
Throws an error if inputted types do not match inputted argments in order.
Typer.TupleAssertion("number", "boolean", "string")(1, true, "Alo") -- No error
Typer.TupleAssertion("boolean", "table")(1, 2) -- Throws error
Expect
Typer.Expect(...: any--
The arguments to be checked
) → ExpectInstance--
The table of methods to be called back, each returning itself
A wrapper for Typer.TupleAssertion, allowing callbacks for different outcomes.
Typer.Expect(1, 2, 3).ToBe("number", "number", "number").AndIfNot(function()
print("Uh oh!")
end).AndIfSo(function()
print("Yay!")
end) -- Prints "Yay!"
Typer.Expect(true, false, true).ToBe("number", "number", "number").AndIfNot(function()
print("Uh oh!")
end).AndIfSo(function()
print("Yay!")
end) -- Prints "Uh oh!"
Env
Typer.Env() → Environment--
The created proxy table
Creates a proxy table that allows access to values stored in it, but does not allow edits to values already declared. It also can be called like a function with a command indicator. Commands are: DumpAll, DumpValue, PrintAll, PrintValue.
local Environment = Typer.Env()
Environment.Foo = 5
Environment.Bar = 10
print(Environment.Foo) -- Prints "5"
print(Environment.Bar) -- Prints "10"
print(Environment.DoesntExist) -- Prints nil and throws warning
Environment("DumpAll") -- Dumps all values, turning them to nil
Environment.Foo = 5
print(Environment.Foo) -- Prints "5"
print(Environment.Bar) -- Prints nil and throws warning
Environment.Foo = 10 -- Throws error since it already exists