Loomguides

TypeScript setup

Ambient globals, JSX intrinsics and module declarations so your editor understands a preview. None of it affects what renders.

This page is for your editor and CI. A preview renders whether or not it typechecks, because esbuild strips types. You’ll add a second tsconfig for the preview, next to the one that builds your Luau.

The ambient globals

Add one side-effect import to declare the Roblox globals and the JSX elements:

src/loom-env.d.ts
import "@loom-dev/preview/globals";

Put the file inside the directory your preview tsconfig includes. It declares the datatypes (UDim2, Color3, Rect, CFrame, TweenInfo, Enum, Instance, game and the rest) and the Luau environment (task, math, string, os, coroutine, typeIs, pcall, pairs, tostring, error, warn).

Two things look odd but are deliberate:

  • print is not declared, because lib.dom already declares one and redeclaring it is an error. print(...) still works at runtime; it just types as the DOM’s zero-argument version.
  • string is also a global value, Luau’s string library. It doesn’t change the string type.

A real roblox-ts project gets similar declarations from @rbxts/compiler-types. Don’t include both in one program.

JSX intrinsics

The same import declares the lowercase elements. The set is exactly what the renderer implements:

screengui surfacegui billboardgui
frame scrollingframe canvasgroup viewportframe videoframe
textlabel textbutton textbox
imagelabel imagebutton
uilistlayout uigridlayout uitablelayout uipagelayout
uipadding uicorner uistroke uishadow uiscale uigradient
uiaspectratioconstraint uisizeconstraint uiflexitem

Anything else is a type error and won’t render. See Supported instances and properties.

Teaching the editor about @rbxts/*

Add ambient module declarations that mirror the plugin’s runtime aliases:

src/loom-aliases.d.ts
declare module "@rbxts/react" {
export * from "react";
export { default } from "react";
}
declare module "@rbxts/react/jsx-runtime" {
export * from "react/jsx-runtime";
}
declare module "@rbxts/react-roblox" {
export * from "@loom-dev/preview/client";
}

Then the preview tsconfig:

tsconfig.json for a preview app
{
"compilerOptions": {
"noEmit": true,
"jsx": "react-jsx",
"moduleResolution": "bundler",
"types": ["vite/client", "react"]
},
"include": ["src"]
}

jsx: "react-jsx" matches the automatic JSX runtime the plugin uses, so you don’t need a React import.

React.Event and React.Change work at runtime but type as errors, because @rbxts/react re-exports plain React’s types. Use the Event={{ Activated: fn }} prop form instead.

Workspace packages and your own aliases

If your preview app imports your UI library by workspace name, the library needs no paths entry. TypeScript follows the workspace link, and loom resolves the package to its TypeScript source at runtime.

The plugin also reads baseUrl and paths from your tsconfig.json, so non-relative imports like import { Button } from "@/ui/Button" resolve at runtime too.

Typechecking is optional

A preview that never runs tsc still renders. Adding tsc --noEmit to CI catches broken scenes early, at the cost of a second tsconfig to maintain. It’s worth it when your previews double as documentation.