ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TypeScriptToLua 类型定义的编写

2022-04-23 13:32:44  阅读:228  来源: 互联网

标签:function ts number module TypeScriptToLua 类型定义 export 编写 declare


_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

标签:function,ts,number,module,TypeScriptToLua,类型定义,export,编写,declare
来源: https://www.cnblogs.com/rongfengliang/p/16182189.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有