TypeScriptToLua
raw JSON → 1.27.0 verified Fri May 01 auth: no javascript
A generic TypeScript to Lua transpiler, version 1.27.0. Write TypeScript code and transpile it to Lua for use in environments like FiveM. This fork modifies class construction from metatables to functions inside the table. Releases follow TypeScriptToLua upstream with some delays. Key differentiators: transpiles TypeScript to Lua for game modding (FiveM, Garry's Mod), leverages full TypeScript tooling (editor support, ESLint, Prettier). Requires TypeScript 5.6.2 as a peer dependency. Active development with regular updates.
Common errors
error Error: Cannot find module 'typescript-to-fivem-lua' ↓
cause Package not installed or npm install failed.
fix
Run 'npm install typescript-to-fivem-lua typescript@5.6.2'
error TypeError: luaLib is not a function ↓
cause luaLib is imported as default instead of named export.
fix
Change import to: import { luaLib } from 'typescript-to-fivem-lua'
error SyntaxError: Unexpected token 'export' ↓
cause Attempting to use ESM syntax with CommonJS require or non-ESM context.
fix
Use import statements or set type: 'module' in package.json, or switch to dynamic import.
Warnings
breaking This fork changes class construction from metatables to functions inside the table. Existing Lua code expecting metatable-based classes will break. ↓
fix Update any Lua interop code that relies on metatable class structure to handle function-based classes.
deprecated The 'luaTarget' option may be deprecated in future versions; prefer 'luaVersion'. ↓
fix Use 'luaVersion' instead of 'luaTarget' in transpile options.
gotcha TypeScript standard library features (e.g., Map, Set, Array methods) may not be supported unless Lua lib is injected. ↓
fix Include luaLib: 'inherit' in transpile options to inject necessary Lua polyfills.
breaking Version 1.25.0 dropped Node <16 support. ↓
fix Upgrade Node.js to version 16.10.0 or later.
Install
npm install typescript-to-fivem-lua yarn add typescript-to-fivem-lua pnpm add typescript-to-fivem-lua Imports
- tslua wrong
const tslua = require('typescript-to-fivem-lua')correctimport tslua from 'typescript-to-fivem-lua' - TranspileOptions
import { TranspileOptions } from 'typescript-to-fivem-lua' - luaLib wrong
import luaLib from 'typescript-to-fivem-lua'correctimport { luaLib } from 'typescript-to-fivem-lua' - transpileString
import { transpileString } from 'typescript-to-fivem-lua'
Quickstart
import { transpileString } from 'typescript-to-fivem-lua';
const tsCode = `
function greet(name: string): string {
return `Hello, ${name}!`;
}
`;
const luaCode = transpileString(tsCode, {
luaTarget: '5.3',
noHeader: true,
});
console.log(luaCode);
// Output:
// function greet(name)
// return "Hello, " .. name .. "!"
// end