Using Existing Luau
roblox-ts lets you use existing Luau scripts in your TypeScript codebase. All you have to do is define the types in a type declaration file (.d.ts
).
Creating Type Declaration Files
roblox-ts will copy any non-compiled file (anything that isn't a .ts
or .tsx
file) into your outDir
(out). This lets you place .lua
files in your rootDir
(src) and they will be copied into the outDir
(out) and then synced into Roblox Studio using Rojo.
To create a type declaration file (.d.ts
) for this .lua
file, simply create a file next to it using the same name.
As an exception, if the name of your .lua
file is init.lua
, your type definition file needs to be named index.d.ts
.
Modeling Module Return Values
Luau scripts return a single value at the end in the form of:
To model this return value in TypeScript types, we can use an "Export Assignment" statement:
And you could import it as:
If a module uses a table return containing static fields/functions, it may be preferrable to use normal exports.
Then your types could look like this:
And you could import them as:
Typings for Custom Classes
A common way to describe custom OOP classes is with a MyClass
interface, MyClassConstructor
interface, and a MyClass
variable of type MyClassConstructor
.
Then in another file, you can use this as: