admin管理员组

文章数量:1394772

I need to know if it's possible to import a Java object (specifically, an enum class) in a Typescript script.

I've googled but haven't found anything.

The ErrorCodeAuthority is for having custom, standardized errors thrown from our service for each known error with set messages (some parameterized, some not), http status codes, etc defined in one place.

In our javascript code we have

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

Is it possible to do the same in Typescript?

I've declared the following:

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

and I'm attempting to use the new type as follows:

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

I'm getting the following error when I try to use grunt to pile to js:

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

(For reference, the super HttpResult is an object that contains a number http code and a stringbody. HttpStatus, in the Java enum, is of typeorg.springframework.http.HttpStatus`).

I tried removing the export function type(arg: ".domain.ErrorCodeAuthority"): ErrorCodeAuthority; line but that didn't change the exception.

We're running all of this inside a nashorn container if that makes a difference

I need to know if it's possible to import a Java object (specifically, an enum class) in a Typescript script.

I've googled but haven't found anything.

The ErrorCodeAuthority is for having custom, standardized errors thrown from our service for each known error with set messages (some parameterized, some not), http status codes, etc defined in one place.

In our javascript code we have

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

Is it possible to do the same in Typescript?

I've declared the following:

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

and I'm attempting to use the new type as follows:

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

I'm getting the following error when I try to use grunt to pile to js:

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

(For reference, the super HttpResult is an object that contains a number http code and a stringbody. HttpStatus, in the Java enum, is of typeorg.springframework.http.HttpStatus`).

I tried removing the export function type(arg: ".domain.ErrorCodeAuthority"): ErrorCodeAuthority; line but that didn't change the exception.

We're running all of this inside a nashorn container if that makes a difference

Share Improve this question edited Apr 21, 2023 at 15:19 E_net4 30.1k13 gold badges114 silver badges151 bronze badges asked Dec 15, 2014 at 20:16 StormeHawkeStormeHawke 6,2176 gold badges48 silver badges74 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

Is it possible to do the same in Typescript?

Yes. With tyrian, you can just write

let JavaErrorCodeAuthority = .domain.ErrorCodeAuthority

And there will be auto-pletion on each level of packages.

Yes, if you already did this in JavaScript you can use the code by creating a definition file for it and port it to TypeScript. An example might be like this:

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

The enum and the first type function with the ".domain.ErrorCodeAuthority" is optional but it gives you better typeinfo when passed in that particular string. Note the declare module part doesn't generate any code and you can add it to a .ts or .d.ts file. More info about creating a definition file can be found here: https://github./Microsoft/TypeScript/wiki/Writing%20Definition%20Files

EDIT

after the info from the ments I hope this code below will better suite your need. This has the downside that it isn't usable in a switch statement but in this case I think it is better to see the java enum as a module (or class was possible to). This might not be 100% correctly modelled but hopefully it gives you some extra idea's. Just a small side note, I find your case very interesting and challenging!

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: ".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(".domain.ErrorCodeAuthority");
new HttpFailResult(JavaErrorCodeAuthority.BAD_REQUEST, null);

本文标签: javascriptNashorn Import Java object in TypescriptStack Overflow