Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Java object in Typescript

To the downvoters - this is a legitimate question. Please take the time to examine it before assuming I'm mixing up my languages like some kind of programming newb!

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("com.domain.ErrorCodeAuthority");

Is it possible to do the same in Typescript?

Edit based on answer below

I've declared the following:

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");

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 compile 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 astringbody. HttpStatus, in the Java enum, is of typeorg.springframework.http.HttpStatus`).

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

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

like image 976
StormeHawke Avatar asked Dec 15 '14 20:12

StormeHawke


2 Answers

Is it possible to do the same in Typescript?

Yes. With 1c, you can just write

let JavaErrorCodeAuthority = com.domain.ErrorCodeAuthority

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

like image 109
wizawu Avatar answered Sep 21 '22 10:09

wizawu


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: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;
    export function type(arg: string): any;
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");

The enum and the first type function with the "com.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.com/Microsoft/TypeScript/wiki/Writing%20Definition%20Files

EDIT

after the info from the comments 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: "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);
like image 36
Dick van den Brink Avatar answered Sep 21 '22 10:09

Dick van den Brink