Type definitions for using the minetest API
Install dev dependency in your typescript project:
npm i @repcomm/mt-api --save-dev
And use:
import type {} from "@repcomm/mt-api";
The module declares the minetest global the same way you'd use it in lua
you can also utilize the types it provides in your own code by importing them:
import type { MtVec3 } from "@repcomm/mt-api";
let myVec: MtVec3 = { x: 0, y: 0, z: 0 };
- minetest global namespace
minetest.register_on_joinplayer((player)=>{
let playername=player:get_player_name();minetest.chat_send_player(playername,"Welcome!")});Users of minetest's lua api will noticed a lack of ":" in typescript
Lua uses obj:method and obj.func to differentiate with obj is passed as self as the first argument
For instance:
localobj= {
method=function (self)
--"self" refers to obj, similar to "this" in typescriptendfunc=function ()
--no self variable hereend
};
obj.method() -- self will be nilobj:method() -- self will be objobj.func() -- self will be nilobj:func() -- self will still be nil because its not declared in function argsIn typescript this is handled by providing a this definition:
interfaceMinetestGlobal{register_on_joinplayer(this: void,cb: MtPlayerJoinCallback): void;}declare global minetest: MinetestGlobal;Because
this: voidTypeScript calls to minetest.register_on_joinplayer() will properly output:
minetest.register_on_joinplayer() in lua
Without providing this: void, this would generate:
minetest:register_on_joinplayer() as typescript-to-lua compiler assumes we want to provide a self reference as first argument
On the flip-side:
functionhandle_player_join (player) --player is ObjRefplayer:get_player_name() -- passes player as first arg to get_player_name codeendminetest.register_on_joinplayer ( handle_player_join )In typescript definitions:
interfaceObjRef{//implicit this: ObjRefget_player_name(): string;//same asget_player_name(this: ObjRef): string;}Which both properly output:
player:get_player_name()