6

Here is an example

The basic idea is - I have a type which can be plain object, or array of objects.

type SingleOrArray<T> = T | T[];

And I have structure like this:

const area: ItemArea = [
  { name: 'test1' },
  { name: 'test2' },
  [
    { name: 'test3' },
    { name: 'test4' },
    [
      { name: 'test5' },
      { name: 'test6' },
      [
        { name: 'test7' },
      ]
    ]
  ]
];

How I can restrict each element of this nested array with:

type Item = { name: string };

thx

1 Answer 1

12

You can't use type because type cannot repeat itself. But you can use interface

This is the interface match your area constant

interface ItemArea {
    [n: number]: {
        name: string
    } | ItemArea;
};
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.