hashids typescript lua 定义文件

定义

package.json

{
  "name": "@dalongrong/myhashids",
  "version": "1.0.0",
  "main": "index.js",
  "types": "hashids.d.ts",
  "license": "MIT",
  "devDependencies": {
    "typescript": "^4.6.3"
  }
}

hashids.d.ts

declare module "hashids" {
    interface Hashids {
        encode(...args: number[]):string
        decode(hashid:string):number[]
    }
    let exports: {
        new: (this: void, saltKey: string,length?:number) => Hashids;
    };
    export default {
        ...exports
    }
    export = {
       ...exports
    }
}

使用

可以先将上边的发布为npm 包,可以使用本地模式 yarn link

  • ts 配置
{
  "tstl": {
    "luaTarget": "JIT",
    "buildMode": "library",
    "noResolvePaths":["platformlogin","@dalongrong/platform-login/dalong","hashids"]
  },
  "compilerOptions": {
    "typeRoots": ["./node_modules/@dalongrong"],  // 配置自己的types 位置
    "target": "esnext",
    "outDir": "dist",
    "module": "commonjs",                                /* Specify what module code is generated. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

ids.ts

// 注意使用 * 
import * as hashids from "hashids"
let id =  hashids.new("demoapp")
console.log(id.encode(3333))

编译效果

 

 

说明

以上是关于ts hashids 类型定义的一个实践,后期计划会创建一个关于openresty 模块的repo,对于常用模块实现一个共享,会复用
社区的一个实现

参考资料

https://github.com/leihog/hashids.lua
https://typescripttolua.github.io/docs/advanced/writing-declarations#operator-overloads
https://github.com/andrei-markeev/openresty-lua-types

posted on 2022-04-26 23:20  荣锋亮  阅读(47)  评论(0编辑  收藏  举报

导航