love-typescript-template icon indicating copy to clipboard operation
love-typescript-template copied to clipboard

[Question] How do you use external Lua libs with this setup?

Open Alexintosh opened this issue 1 year ago • 1 comments

Pretty much what's in the title.

Alexintosh avatar Dec 30 '23 13:12 Alexintosh

To import some Lua code for a project, you'll need the .lua and maybe a handwritten .d.ts file.

See External Code (TypeScriptToLua docs) for more.

File structure

Since the project will have handwritten .d.ts files worthy of tracking, I think a lib directory in src would be okay.

 src/
+  lib/
+    inspect.d.ts
+    inspect.lua
   conf.ts
   main.ts

And an additional .gitignore rule of src/lib/*.lua would prevent publishing Lua code that isn't managed by this project. But sometimes a Lua library might require you to change the require paths manually which would be a change to track.

If you know of TypeScript declarations for this Lua library, use those instead and refer to the instructions they provide.

Writing declarations

Because I know inspect.lua provides a function called inspect I can use this in inspect.d.ts.

export function inspect(root: object): string;

And then import it like this in main.ts.

import { inspect } from "./lib/inspect";

print(inspect([1, 2, 3]));

Check if it runs in the project and if it does that's enough to use this new library!

TypeScriptToLua will handle the rest including copying the .lua file too!

See Writing Declarations (TypeScriptToLua docs) if you're not getting the right output from TypeScriptToLua.

Publishing?

If you end up with a complex library that you were willing to write, you may want to consider publishing them to NPM. A name like <package>-types is one that's been used with TypeScriptToLua declarations.

hazzard993 avatar Jan 01 '24 14:01 hazzard993