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   荣锋亮  阅读(55)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-04-26 cube.js 自定义日志处理
2021-04-26 tendis nodejs 连接问题
2021-04-26 nodejs 异步转同步整理
2020-04-26 fusionauth 通用sso 解决方案学习二 基本试用
2020-04-26 zeeqs 一个通用的zeebe 数据查询服务
2020-04-26 zeebe 0.23.1 变动说明
2020-04-26 fusionauth 通用sso 解决方案学习一 环境运行

导航

< 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
点击右上角即可分享
微信分享提示