TypeScriptToLua 类型定义的编写

_G.d.ts

类似全局定义文件(global.d.ts)
参考

 
declare namespace me {
    var dalong_age:number
    function demo():string
    function print(...args:any[]):void
}

使用 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

posted on   荣锋亮  阅读(184)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用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 仓库

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示