mt-api
Type definitions for using the minetest API
using
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 };
implemented
- minetest global namespace
minetest.register_on_joinplayer( (player)=>{
let playername = player:get_player_name();
minetest.chat_send_player(playername, "Welcome!")
} );
dev-dependencies
contributors (any contributions welcome, thank you!)
also see
contributing
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:
local obj = {
method = function (self)
--"self" refers to obj, similar to "this" in typescript
end
func = function ()
--no self variable here
end
};
obj.method() -- self will be nil
obj:method() -- self will be obj
obj.func() -- self will be nil
obj:func() -- self will still be nil because its not declared in function args
In typescript this is handled by providing a this
definition:
interface MinetestGlobal {
register_on_joinplayer (this: void, cb: MtPlayerJoinCallback): void;
}
declare global minetest: MinetestGlobal;
Because
this: void
TypeScript 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:
function handle_player_join (player) --player is ObjRef
player:get_player_name() -- passes player as first arg to get_player_name code
end
minetest.register_on_joinplayer ( handle_player_join )
In typescript definitions:
interface ObjRef {
//implicit this: ObjRef
get_player_name(): string;
//same as
get_player_name(this: ObjRef): string;
}
Which both properly output:
player:get_player_name()