MTASA Typescript
I am developing utility packages for transpiling TypeScript into an MTASA-compatible Lua code. I would like to share that project to receive some suggestions and, I believe, to help someone in resource writing.
Why Lua is not so good
Classes. Lua does not support classes (syntax) from scratch. If you want to write OOP code with your own classes, you have to use metatables to implement class objects behavior.
That's make the source code more complicated, than it could be.
Hints (from your IDE). Lua is dynamic typed language, therefore, it's hard to determine, what the variable is and, hence, IDE cannot hint you function declarations, object methods and etc.
MTASA Lua is still 5.1 (it is not developing). Lua, like any programming language, is developing, however MTASA core strongly linked to Lua 5.1.
Why TypeScript
There is TypeScriptToLua project. My project is based on TypeScriptToLua (I use it as a dependency). General purpose of TypeScriptToLua: convert (transpile) typescript code into Lua code. Thus, you can use TypeScript without modifying MTASA core.
You are available to not use types in some parts of code (you are not restricted to use types everywhere).
The TypeScript is being developed by Microsoft and, I believe, that TypeScript will be alive for several years.
When to not use TypeScript (where to use Lua instead of TypeScript)
1. If you are not familiar with Lua or with scripting in general.
2. if you are writing simple script.
Introduction Video
I have made the video, where I describe Lua disadvantages, explain, why I have chosen TypeScript (and language transpiling) and write an example.
Few word about mtasa-lua-types
I have created this package to provide MTASA declarations in TypeScript. If you would like to create a resource, you haven't to install it manually, just use the boilerplate.
Quick Start
I have provided the tutorial here: https://github.com/mtasa-typescript/resource-boilerplate.
So, you have to install NodeJS and download my boilerplate. And,... you are ready to code.
If you have any ideas, issues or suggestions -- post them below, or in youtube comments, or in github issues ?
Example code (from youtube video)
import { Player } from 'mtasa-lua-types/types/mtasa/server/oop/Player';
import { mtasa } from 'mtasa-lua-types/types/mtasa/server';
interface GithubCommitApiResponse {
commit: {
message: string;
}
}
mtasa.addCommandHandler('getcommit', function(player: Player, cmd: string) {
mtasa.fetchRemote(
'https://api.github.com/repos/mtasa-typescript/mtasa-lua-types/commits?per_page=1',
(data: string) => {
const json = mtasa.fromJSON(data) as GithubCommitApiResponse
mtasa.outputConsole(`The last commit message: ${json.commit.message}`)
}
)
})
Future plans
- Github bot, that will crawl mtasa wiki and update mtasa-lua-types continuously
- Add better hinting for `addEventHandler` function
Links
WebSite: https://mtasa-typescript.github.io/
GitHub MTASA TypeScript Projects: https://github.com/mtasa-typescript
Resource Boilerplate: https://github.com/mtasa-typescript/resource-boilerplate
Resource Example: https://github.com/mtasa-typescript/resource-example