1. 程式人生 > 其它 >graalvm typescript types 編寫定義參考說明

graalvm typescript types 編寫定義參考說明

以下只是一個簡單的學習,大家可以參考,然後基於此擴充套件

案例說明

就是一個簡單的java.math.BigInteger 定義

參考定義

  • package.json
{
  "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"
  }
}
  • tsconfig.json
{
  "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. */
  }
}
  • index.d.ts
// 型別定義,類似一個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]
}

專案使用

  • package.json
{
  "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. */
  }
}
  • src/app.ts
let app = Java.type("java.math.BigInteger")
console.log(app.valueOf(BigInt("33")).pow(33).toString(16))
  • 安裝graalvm nodejs
    需要安裝graalvm,以及nodejs 可以使用如下命令 (通過sdkman),graalvm 21 之後nodejs 需要獨立安裝
 
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/