Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deep nested types

I am newby in typescript. therefore could someone describe me how to realize this type of deep nested object programatically:

type typeDeepNestedGeneric<T> = T
    | {[key: string]: T}
    | {[key: string]: {[key: string]: T}}
    | {[key: string]: {[key: string]: {[key: string]: T}}}
    | {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}
    | {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}
    | {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}}
    | {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}}}

in my class object I want to define properties with objects with unlimited depth of types included:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Does this satisfy?


type DeepNestedGeneric<T> = T | {[key: string]: DeepNestedGeneric<T> }

const x : DeepNestedGeneric<string> = {
     foo: "wsdfhsdf",
     bar: {
          whatever: "akjdhakjs",
          baz: {
               blerg: "dsfsdsdfsd",
               errors: 5 // Type 'number' is not assignable to type 'DeepNestedGeneric<string>'.
          }
     }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading