TypeScriptToLua 类型定义的编写
_G.d.ts
类似全局定义文件(global.d.ts)
参考
declare namespace me {
var dalong_age:number
function demo():string
function print(
}
使用 me.print("dalong")
export 关键字
实际上也是标准的ts 定义,对于export 的需要导入(import),包含了几种模式,export = , export default, export {}
lib.d.ts
export var name_version:number
使用
import demo from "./lib"
demo.name_version
如果namepspace 包含了export 会告诉ts ,可以在namespace 中访问
模块定义
对于模块我们是需要import 的
比如
utf8.d.ts
declare module "utf8" {
/**
* @noSelf
*/
export function codepoint(): void;
}
使用
import * as utf8 from "utf8"; // equiv to `local utf8 = require("utf8");
utf8.codepoint();
self 参数问题
ts 包含了一个隐藏的this,但是很多时候我们的lua 是不需要的,解决方法
this:void
@noSelf
@noSelfInFile
参考
declare namespace table {
export function remove(this: void, table: object, index: number): any;
}
/** @noSelf */
declare namespace table {
export function remove(table: object, index: number): any;
}
/** @noSelfInFile */
declare namespace table {
export function remove(table: object, index: number): any;
}
实际上TypeScriptToLua 也包含了全局参数
@noResolution 参数
使用此参数可以让TypeScriptToLua 不去解析引用的模块
比如
/** @noSelf */
declare module "image-size" {
export function getimagewidth(filename: string): number;
export function getimageheight(filename: string): number;
}
/**
* A module that only contains a number
* @noResolution
*/
declare module "number-of-the-day" {
let x: number;
export = x;
}
/**
* Not very useful for TypeScript. It has no idea what is in here.
* @noResolution
*/
declare module "custom-module";
保留关键字处理
比如try, catch,new 解决方法是定义为一个json 对象
参考
declare let table: {
new: () => any;
};
declare module "creator" {
let exports: {
new: () => any;
};
export = exports;
}
操作符重载
lua 支持操作符重载+,-,* 具体是通过元表操作的 __add,__sub,__mul,__div,__unm
可以自己编写,或者基于lua 扩展
其他功能
与ts 的类型类似比如Unions ,keysof
参考资料
https://typescripttolua.github.io/docs/advanced/writing-declarations
https://typescripttolua.github.io/docs/advanced/language-extensions
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-04-23 edgedb 集成timescaledb
2019-04-23 edgedb 内部pg 数据存储的探索 (四) 源码编译
2019-04-23 edgedb 内部pg 数据存储的探索 (二) 创建数据库命令说明
2019-04-23 edgedb 内部pg 数据存储的探索 (三) 源码包setup.py 文件
2019-04-23 python 集成cython && push 测试pip 仓库