I am using a mapped type in my typescript source, that I am reusing throughout my project:
type StringUnion = string | string[];
type StringUnionMap = { [key: string]: StringUnion };
I have to combine two of these maps into one map that contains the entries of both:
let map1: StringUnionMap = { "key1": "value1", "key2": "value2" };
let map2: StringUnionMap = { "key3": [ "value3a", "value3b" ] };
I can of course achieve what I want in a loop like this:
let combinedMap: StringUnionMap = {};
for (let key in map1) {
combinedMap[key] = map1[name];
}
for (let key in map2) {
combinedMap[key] = map2[name];
}
I find this way a bit clumsy and verbose. Is there any more concise way to combine my two maps?
I have tried this, but that does not compile transpile:
let combinedMap: StringUnionMap = new StringUnionMap( [ map1, map2 ] );
The respective JavaScript solution with Object.assign does not work, because I am targeting ES5 with noImplicitAny turned on. Therefore Object.assign is not available to me.
Object.assignor its equivalent, which is a pure ES6 construct.Map. Anyway, your "construct" does exist in JS. All you have done is applied TS typing to a JS object, which doesn't change the fact that it is a JS object. I suggest you go back and carefully study the role of TS in the language ecosystem.