1

For this raw data (originally a JSON file and imported by import * as raw_data from './json_file.json'):

let raw_data: object = {"a": {"name": "Name a", "desc": "Description a"}, "b": {"name": "Name b", "desc": "Description b"} }

I would like to retrieve a map of keys (as strings) and the custom objects of interface AnObject

interface AnObject {
    name: string;
    description: string;
}

in typescript. How can I do that?

I have tried

let myObject: Map<string, AnObject> = raw_data as Map<string, AnObject>;

or

let myObject: Map<string, AnObject> = <Map<string, AnObject>>raw_data

but with small success: The object remains an object and myObject.size is undefined.

1 Answer 1

5

You are trying only type level modifications, but an object is actually different from a Map after transpilation, so you need to create it like this :

let myObject: Map<string, AnObject> = new Map(Object.entries(raw_data));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.