Roblox API
roblox-ts provides typings for the Roblox API which are partially handwritten and partially automatically generated.
In general, everything from the Roblox API is available for use, unless it is deprecated or conflicts with TypeScript.
Values
Globals
All global values from the Roblox API are present in roblox-ts typings.
You can find a list of available globals on the Roblox Developer Hub:
Parts of table
and string
have been intentionally omitted.
Constructors
.new()
functions (like Vector3.new()
or CFrame.new()
) should instead be called with the new
operator.
new T(...)
will always compile to T.new(...)
.
nil
undefined
is a direct replacement for nil
. It can be used both as a type and a value.
Types
Provided Types
Every Roblox class (Instance
, Part
, Humanoid
, Workspace
, etc.) is provided as a global/ambient type. You can use these types to describe variables, function parameters, function return types, and just about anything else in your code.
RemoteEvent Types
New roblox-ts users are usually confused why RemoteEvent.OnServerEvent
only allows unknown
arguments.
The reason for this is because client-to-server networking cannot be trusted. Exploiters or cheaters in your game can fire your RemoteEvent functions with whatever data they want.
If your code is expecting points
to be a number
, but the client sends a nil
value, the server will error. By sending a large number of requests very quickly which result in errors, an exploiter could crash your game server and disconnect all of your players.
Instead, you should assume your inputs can be any possible value. You can verify the arguments with a type validation package like @rbxts/t
.
Alternatively, the community has created a few networking libraries which make the experience much nicer!
Exceptions
Deprecated types are usually not provided. Exceptions to this rule are made for API members which do not have a non-deprecated functional equivalent.
One notable exception: Instance.Changed
is not provided as it conflicts with inheritance. Usually, you want to use Instance.GetPropertyChangedSignal()
instead.
If you must use Instance.Changed
, you can workaround this by asserting an intersection type with ChangedSignal
:
Utility Interfaces
There are a few key global interfaces which make manipulating types easier:
Services
Services
is an interface consisting of a mapping of string name to type for every Roblox service which you can fetch with game:GetService("ServiceName")
.
You can get a union of all service names with keyof Services
.
And you can get a union of all service types with Services[keyof Services]
.
CreatableInstances
CreatableInstances
is an interface consisting of a mapping of string name to type for every Roblox instance which can be created with Instance.new("ClassName")
.
You can get a union of all creatable instance names with keyof CreatableInstances
.
And you can get a union of all creatable instance types with CreatableInstances[keyof CreatableInstances]
.
AbstractInstances
AbstractInstances
is an interface consisting of a mapping of string name to type for every Roblox instance which will never be created. Generally, these are useful for functions that check inheritance like Instance:IsA("ClassName")
.
You can get a union of all abstract instance names with keyof AbstractInstances
.
And you can get a union of all abstract instance types with AbstractInstances[keyof AbstractInstances]
.
Instances
Instances
is an interface consisting of a mapping of string name to type for every Roblox instance. It inherits from Services
, CreatableInstances
, and AbstractInstances
. Instances
and also includes any type of Instance which:
- cannot be created with
Instance.new("ClassName")
- cannot be fetched with
game:GetService("ServiceName")
- but can be given a reference to
Examples:
DataModel
is a class that cannot be created or fetched with GetService, but is given by thegame
global valueAnimationTrack
can only be given a reference to via:LoadAnimation()
fromHumanoid
orAnimationTrack
You can get a union of all instance names with keyof Instances
.
And you can get a union of all instance types with Instances[keyof Instances]
.
Usage with Generics
You can use any of these utility interfaces with generic functions. This is useful for taking an argument string and returning a matching instance type.