Saurabh Kumar
Saurabh Kumar

Reputation: 16661

How to Map Java Type to TypeScript/javascript

I made a corresponding UI object as follows. Now the backend Target java object has a field like following. Is there any way i can define similar in Angular Target object.Is there set and Map object implementation in Angular2

Java

Not javascript

private Set<String> exportCodes = new HashSet<>();
private Map<String, Set<String>> itemDataTypeMap = new HashMap<>();

Corresponding TypeScript object

export class Target {
    constructor(
        public name?: string,
        public targetType?: string,
        public exportUrl?: string,
        public userName?: string,
        public password?: string,
    ) { }
}

Upvotes: 0

Views: 2366

Answers (2)

Saurabh Kumar
Saurabh Kumar

Reputation: 16661

    I used. This works. tested

MAP

    interface Map
    var Map: MapConstructor

SET

    declare var Set: SetConstructor;

conversions might look like following. I still need to check with data.

    public exportCodes?: Set<string>,
    public itemDataTypeMap?: Map<string, Set<Item>>

Upvotes: 1

n00dl3
n00dl3

Reputation: 21564

There are some Set and Map classes in ES6-enabled browsers, you should just need to set the target of your tsconfig.json to ES6.

There are also polyfills for old-school browsers. like core-js

You should then be able to declare such objects like:

let foo = new Map<string,Set<string>>();
let bar = new Set<string>();

Also note that the traditional way of creating a Map<string,something> in js is to make an object that will act as an associative array, it has the advantage of being portable across old and modern browsers. You can declare this in typescript like a dictionnary:

let foo:{[k:string]:any}={}

Upvotes: 1

Related Questions