FiveM TypeScript Usage Guide

This guide covers TypeScript development for FiveM with key differences from Lua.

⚠️ Critical Import Requirement

IMPORTANT: All scripts MUST be imported in their respective main.ts files to be built.

Client-side (src/client/main.ts):

/// <reference types="@citizenfx/client" />
import './my-script';

Server-side (src/server/main.ts):

/// <reference types="@citizenfx/server" />
import './my-script';

Basic Types

Variables

Lua:

local playerName = "John"
local playerHealth = 100
local isAlive = true

TypeScript:

/// <reference types="@citizenfx/client" />

let playerName: string = "John";
const playerHealth: number = 100;
const isAlive: boolean = true;

Arrays

Lua:

TypeScript:

Functions

Lua:

TypeScript:

Events

Lua:

TypeScript:

Tables vs Objects

Lua:

TypeScript:

FiveM-Specific Types

Vector Types

Entity Types

Key Differences

Type References

TypeScript requires type references at the top of every file:

Import/Export System

Lua (no imports needed):

TypeScript (must import):

Event System: on vs onNet

on - For client/server specific events:

onNet - For cross-side events (can be triggered from either side):

Commands

Lua:

TypeScript:

Error Handling

Lua:

TypeScript:

Best Practices

  1. Always add type references at the top of files

  2. Import all new files in main.ts

  3. Use TypeScript interfaces for data structures

  4. Handle errors with try-catch blocks

  5. Use const for values that don't change

  6. Use let for values that do change

  7. Avoid var (it's function-scoped, not block-scoped)

Last updated