Typescript create anonymous objects

Viewed 32565

I want to create a hierarchy of nested objects in typescript that looks like the following

snapshot{
   profile{
      data{
         firstName = 'a'
         lastName = 'aa'
      }
   }
} 

I dont want to create a class structure, just want to create the nested hierarchy of objects thats all.

2 Answers

If you want TypeScript to enforce your anonymous object type, you can do the following. However, I recommend using this technique sparingly. If you have large complex objects, it will probably benefit you to call out an interface/class structure. Otherwise, the readability of your code may suffer.

let snapshot: {
  profile: {
    data: {
      firstName: string;
      lastName: string;
    };
  };
};
Related