其他分享
首页 > 其他分享> > graalvm typescript types 编写定义参考说明

graalvm typescript types 编写定义参考说明

作者:互联网

以下只是一个简单的学习,大家可以参考,然后基于此扩展

案例说明

就是一个简单的java.math.BigInteger 定义

参考定义

{
  "name": "@dalongrong/graalvm-type-learning",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "types": "dist/index.d.ts",
  "devDependencies": {
    "typescript": "^4.6.4"
  },
  "files": [
    "dist/*.d.ts"
  ],
  "scripts": {
    "app":"tsc",
    "watch":"tsc --watch",
    "p":"yarn publish"
  },
  "publishConfig": {
    "access": "public",
    "registry": "https://registry.npmjs.com"
  }
}
{
  "include": [
    "src/*"
  ],
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "outDir": "dist",
    "declaration": true,
    /* Language and Environment */
    "target": "ESNext",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["DOM","ES2015","ES2020.BigInt"],
    /* Modules */
    "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. */
    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}
// 类型定义,类似一个map
interface JavaType {
    "java.math.BigInteger": typeof Java.math.BigInteger;
}
 
/**
 * just for type definition don't use it directly
 */
declare namespace Java {
    namespace math {
       // BigInteger 定义
        class BigInteger {
             public static valueOf(value:bigint):BigInteger;
             pow(exponent:number):BigInteger;
             toString():string;
             toString(radix:number):string;
        } 
    }
    /**
     *  工具类实现type 的获取
     * @param key get java type must use --jvm with java interop
     */
    function type<Key extends keyof JavaType>(key: Key):JavaType[Key]
}

项目使用

{
  "name": "g-ts",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "app": "tsc --watch"
  },
  "devDependencies": {
    "@dalongrong/graalvm-type-learning": "^1.0.0"
  }
}
``
tsconfig.json
```code
{
  "include": [
    "src/*"
  ],
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "types": ["@dalongrong/graalvm-type-learning"],
    "outDir": "dist",
    "lib": ["ESNext.BigInt","ESNext","DOM"],
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "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. */
  }
}
let app = Java.type("java.math.BigInteger")
console.log(app.valueOf(BigInt("33")).pow(33).toString(16))
 
sdk install java 22.1.0.r17-grl
gu install nodejs
<path to graalvm bin dir >/22.1.0.r17-grl/bin/node --jvm  dist/app.js 

 

 

说明

以上只是一个简单的开头,以及如果集成使用,我们可以扩展下,实际上es4x 已经实现了以上的一些类型能力,而且包含了自动化的工具

参考资料

https://www.npmjs.com/package/@dalongrong/graalvm-type-learning
https://www.graalvm.org/22.1/reference-manual/js/
https://reactiverse.io/es4x/

标签:BigInteger,typescript,type,app,ts,true,types,graalvm
来源: https://www.cnblogs.com/rongfengliang/p/16226561.html