• 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

  • 相关阅读:
    C# Linq 类似Scala中的map的函数
    Spark DataFrame NOT IN实现方法
    Scala scopt 命令行解析
    WPF 绑定到静态属性,可通知
    WPF GroupBox Header居中
    WPF开源项目整理(排名不分先后)
    Windows 上配置 Go 的 gRPC 编译环境
    C++20新线程 jthread 体验代码
    查找被删除但仍然占据磁盘的文件
    以Docker方式安装Redis集群
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/16182189.html
Copyright © 2020-2023  润新知