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?

3

0

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.