Import Java Object in Typescript

Downstream is a legitimate question. Please take the time to learn it before assuming that I am mixing my languages ​​like some newb programming!

I need to know if it is possible to import a Java object (in particular, the enum class) into a Typescript script.

I searched googled but didn't find anything.

ErrorCodeAuthority is designed to create custom standard errors emanating from our service for every known error with given messages (some parameterized, some not), http status codes, etc., defined in one place.

Our javascript code has

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

Is it possible to do the same in Typescript?

Edit based on answer below

I stated 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 am trying 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());
    }
}

, grunt js:

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

( HttpResult - , number http code and a body. HttpStatus, in the Java enum, is of type org.springframework.http.HttpStatus`).

export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;, .

2 nashorn, .

+4
2

, 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" , info . , declare , .ts .d.ts. : https://github.com/Microsoft/TypeScript/wiki/Writing%20Definition%20Files

, , . , switch, , java enum ( ). 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);
+1

Typescript?

. 1c

let JavaErrorCodeAuthority = com.domain.ErrorCodeAuthority

.

+1

All Articles