graalvm typescript types 编写定义参考说明
2022/5/5 23:14:22
本文主要是介绍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/
这篇关于graalvm typescript types 编写定义参考说明的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享