Nashorn:在Typescript中导入Java对象

8

我需要知道在Typescript脚本中是否可以导入Java对象(特别是枚举类)。

我已经搜索过了,但没有找到任何相关信息。

ErrorCodeAuthority用于从我们的服务中抛出自定义的、标准化的错误,每个已知错误都有设置消息(一些参数化,一些不是)、HTTP状态码等,在一个地方定义。

在我们的JavaScript代码中,我们有:

var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");

在TypeScript中是否可以做到同样的事情?

我已经声明了以下内容:

declare module Java {
    export enum ErrorCodeAuthority {
        ENTITY_NOT_FOUND,
        HTTP_VERB_NOT_SUPPORTED,
        BAD_REQUEST,
        //...
    }
    export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;
    export function type(arg: string): any;
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");

我正在尝试使用以下新类型:

export class HttpFailResult extends HttpResult {
    constructor(public errorCode : Java.ErrorCodeAuthority, public userParams? : UserParam[]) {
        super(errorCode.httpStatus.value(), errorCode.toString());
    }
}

当我尝试使用grunt编译为js时,我遇到了以下错误:

error TS2339: Property 'httpStatus' does not exist on type 'ErrorCodeAuthority'.

(供参考,超级 HttpResult 是一个对象,包含一个数字 http code 和一个字符串 body。在 Java 枚举中的 HttpStatus 属于类型 org.springframework.http.HttpStatus
我尝试删除 export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority; 这一行,但这并没有改变异常。
如果需要,我们将所有内容都运行在 nashorn 容器内。

这些踩票是怎么回事? - StormeHawke
1
我不知道(我给你点赞是因为你的评分是-2),我怀疑人们认为你混淆了Java和Javascript之类的东西,而且他们不知道例如https://github.com/joeferner/node-java的存在。 - Dick van den Brink
2个回答

3
能否在TypeScript中做到同样的事情?
可以。使用tyrian,您只需编写
let JavaErrorCodeAuthority = com.domain.ErrorCodeAuthority

每个级别的包都将有自动完成功能。


已经晚了,这对我们来说甚至不再是一个要求,因为我们(幸运的是)放弃了运行混合Java / JavaScript服务器,但还是谢谢。 - StormeHawke

1

是的,如果您已经在JavaScript中完成了这个任务,您可以通过创建一个定义文件并将其转换为TypeScript来使用该代码。例如:

declare module Java {
    export enum ErrorCodeAuthority {
        item1,
        item2
    }
    export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;
    export function type(arg: string): any;
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");

枚举和第一个类型函数中的“com.domain.ErrorCodeAuthority”是可选的,但是当传递特定字符串时,它会提供更好的类型信息。请注意,声明模块部分不会生成任何代码,您可以将其添加到.ts.d.ts文件中。有关创建定义文件的更多信息,请参见此处:https://github.com/Microsoft/TypeScript/wiki/Writing%20Definition%20Files

编辑

在评论中提供的信息之后,我希望以下代码将更好地满足您的需求。 这样做的缺点是它不能在switch语句中使用,但在这种情况下,我认为最好将java枚举视为模块(或者如果可能的话为类)。这可能不是100%正确建模,但希望它能给您一些额外的想法。只是一个小小的副注,我认为您的情况非常有趣而具有挑战性!

declare module Java {
    interface ErrorCodeValue {
       toString(): string;
       value(): number;
    }
    module ErrorCodeAuthority {
        var ENTITY_NOT_FOUND: IErrorCodeAuthority;
        var HTTP_VERB_NOT_SUPPORTED: IErrorCodeAuthority;
        var BAD_REQUEST: IErrorCodeAuthority;
    } 
    interface IErrorCodeAuthority {
        httpStatus: ErrorCodeValue;
    }
    export function type(arg: "com.domain.ErrorCodeAuthority"): typeof ErrorCodeAuthority;
    export function type(arg: string): any;
}
export class HttpResult {
    constructor(code: number, description: string) {        
    }
}
export class HttpFailResult extends HttpResult {
    constructor(public errorCode: Java.IErrorCodeAuthority, public userParams? :any[]) {
        super(errorCode.httpStatus.value(), errorCode.toString());
    }
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");
new HttpFailResult(JavaErrorCodeAuthority.BAD_REQUEST, null);

这样做是否允许我引用,比如说 ErrorCodeAuthority.ITEM_NOT_FOUND.customErrorMessage?(使用Java增强枚举的能力) - StormeHawke
如果您删除 export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority 并将其用作任何类型,那么是可能的。但我认为在类型检查方面不可能实现,因为我认为在 TypeScript 中无法将 ITEM_NOT_FOUND 和 ITEM_NOT_FOUND.httpStatus 结合起来。 - Dick van den Brink
好的,我已经尝试了您上面提供的解决方案,它让我有所进展,但我仍然卡在增强枚举部分。根据您的答案,我在问题中添加了一些额外的细节,能否看一下? - StormeHawke
它必须能够在switch语句中使用吗?(ErrorCodeAuthority.ENTITY_NOT_FOUND) 因为这使得在TypeScript中使用Java枚举风格变得非常困难。 - Dick van den Brink
编辑了我的答案,加入了你的评论和修改后的答案。希望能帮助你解决问题 :) - Dick van den Brink
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接