TypeScriptToLua

raw JSON →
1.0.1 verified Fri May 01 auth: no javascript

A generic TypeScript to Lua transpiler that lets you write TypeScript code and compile it to Lua. This package (v1.0.1) is a fork requiring TypeScript ~4.4.3. It provides a command-line tool (`tstl`) similar to `tsc`. Key differentiators: preserves TypeScript type safety in Lua projects; supports declaration files for existing Lua APIs; integrates with standard TS tooling (ESLint, Prettier). Release cadence is sporadic; check upstream TypeScriptToLua for active development.

error Error: Cannot find module 'typescript-to-lua'
cause Package not installed or missing from node_modules.
fix
npm install -D typescript-to-lua
error TypeError: transpileString is not a function
cause Default import used incorrectly (import transpileString from '...') or CommonJS require used.
fix
Use named import: import { transpileString } from 'typescript-to-lua'
error error TS2688: Cannot find type definition file for 'typescript-to-lua'.
cause TypeScript cannot locate types for the package.
fix
Ensure 'typescript-to-lua' is installed and types are bundled (they are). May need to add 'typescript-to-lua' to 'types' in tsconfig.json.
error The 'transpile' function requires a TypeScript Program object, not source code.
cause Confusing transpile with transpileString.
fix
Use transpileString for string input, or create a ts.Program using ts.createProgram for file-based input.
gotcha This is a fixed fork of the original TypeScriptToLua. Use the original for latest features and updates.
fix Consider using 'typescript-to-lua' (original) instead unless you specifically need this fork's fixes.
gotcha Requires TypeScript ~4.4.3 as a peer dependency. Incompatible with newer TypeScript versions (4.5+).
fix Install TypeScript 4.4.3: npm install -D typescript@~4.4.3
gotcha The 'transpileString' function returns an object with a 'file' property; access 'file.lua' to get the Lua code.
fix Use result.file?.lua to obtain the output.
npm install typescript-to-lua-fixed
yarn add typescript-to-lua-fixed
pnpm add typescript-to-lua-fixed

Demonstrates transpiling a TypeScript class to Lua using 'transpileString' and file-based transpilation with 'transpile'.

import { transpileString } from 'typescript-to-lua';
import { transpile } from 'typescript-to-lua';
import * as ts from 'typescript';

// Example: transpile a simple TypeScript class
const source = `
class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}
`;

const result = transpileString(source, {
    luaTarget: "5.3",
    noHeader: true
});

console.log(result.file?.lua);
// Output: 
// Greeter = {}
// function Greeter.new(self, message)
//   self.greeting = message
//   return self
// end
// function Greeter.greet(self)
//   return "Hello, " .. self.greeting
// end

// For file-based transpilation:
const program = ts.createProgram(['./input.ts'], {});
const emitResult = transpile(program, { luaTarget: '5.3' });
console.log(emitResult.emitSkipped ? 'Emit skipped' : 'Emit succeeded');