0

I have an array and a string which are like:

const array1 = ['id', 'home', 'address']
const string1 = 'isValid'

I want to create an array of objects with some operator(string) in between like the following:

coupon : [
    {
        reference: ['id'],
    },
    '=',
    {
        reference: ['isValid', 'id'],
    },
    'and',
    {
        reference: ['home'],
    },
    '=',
    {
        reference: ['isValid', 'home'],
    },
    'and',
    {
        reference: ['address'],
    },
    '=',
    {
        reference: ['isValid', 'address'],
    },
]

How to create an interface for this, for example if i will add more items in the array1 the array of object will change.

I have created an interface

interface IReference {
    reference: string[]
}

interface IReferences {
    coupon: [IReference, string, IReference] | IReference
}

Now when I add the value of One element which is like shown below, it's not throwing an error.

{
    reference: ['id'],
},
'=',
{
    reference: ['isValid', 'id'],
}

But when i add a string in between 'and'

And then the other elements:

{
    reference: ['home'],
},
'=',
{
    reference: ['isValid', 'home'],
}

It is throwing an error, I think the interface is not correct. How should i achieve this in Typescript with the interfaces?

Usman Irshad
  • 37
  • 1
  • 8
  • 1
    https://stackoverflow.com/questions/61155425/how-to-define-array-with-alternating-types-in-typescript – Roberto Zvjerković Jun 17 '21 at 19:29
  • ` [IReference, string, IReference] | IReference: string[]` is not valid TypeScript. Could you make sure your code constitutes a [mcve] suitable for dropping into a standalone IDE like [the TypeScript Playground (link to code)](https://tsplay.dev/wgrEyW)? – jcalz Jun 18 '21 at 01:03
  • @jcalz : I have updated the example. Its 'IReference' instead of 'IReference: string[]' – Usman Irshad Jun 18 '21 at 07:28

0 Answers0