Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript recursive types

type NestedNumbers = number | NestedNumbers[]
const val: NestedNumbers = [3, 4, [5, 6, [7], 59], 221]
//another example
type RecursiveObject = {
  name: string;
  children?: RecursiveObject[];
};
const recursiveData = [
  {
    name: 'First',
    children: [
      {
        name: 'Second',
        children: [
          {
            name: 'Third',
            children: [
              {
                name: 'Fourth',
                children: [
                  {
                    name: 'Fifth',
                    children: [
                      // On and on...
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
];
//Another example to implement type for json
type JSONPrimitive = string | number | boolean | null
type JSONValue = JSONArray | JSONObject | JSONPrimitive
type JSONObject = { [k: string]: JSONValue }
type JSONArray = JSONValue[]
 
PREVIOUS NEXT
Tagged: #typescript #recursive #types
ADD COMMENT
Topic
Name
4+4 =