1
type subProp = {
  id: string,
  name: string
};

type ParentProps = {
  subscriptions?: [
    {
      id: string,
      items: [???Array Of subProp???]
    }
  ]
}

Is this case doable in typescript with only type alias? If so, how to do it? I can't find any viable option online. If no, what's the alternative?

ey dee ey em
  • 7,991
  • 14
  • 65
  • 121

1 Answers1

2

You can declare items as an array of subProps and subscriptions as an array of a type with id and items:

type subProp = {
  id: string,
  name: string
};

type ParentProps = {
    subscriptions?: {
        id: string,
        items: subProp[];
    }[];  
}
eol
  • 23,236
  • 5
  • 46
  • 64
  • what is this `XXX[ ]` syntax calls? I want to look into it and learn more! thanks – ey dee ey em Jun 30 '20 at 14:43
  • It just means that it's an array of type `XXX`. Another way to do this is: `items: Array`, see: https://www.typescriptlang.org/docs/handbook/basic-types.html#array and https://stackoverflow.com/questions/36842158/arraytype-vs-type-in-typescript – eol Jun 30 '20 at 14:45