2

I'm trying to write the type for an array that contains objects, but with different types.

[
        {
            "id": "test",
            "answer": "1"
        },
        {
            "id": "test_multi",
            "answers": [
                {
                    "id": "skill_1"
                },
                {
                    "id": "skill_2"
                }
            ]
        },
]

My first approaches were:

prop: { id: string; answer: string | boolean | { id: string; answers: { id: string }[]} }[];

I guess in this case I'm assigning the different object to the answer prop.

Next approach

    { id: string; answer: string | boolean;  } | { id: string; answers: { id: string }[] }[];

But in this case I guess I'm not allowing the array to be filled by the first type of object.

How can I write the type for "this array can contain this object AND/OR this object"?

2
  • Your second approach is close, just use parentheses ( ... ) from the beginning until the [] Commented Aug 19, 2021 at 14:13
  • Or Array<Foo | Bar> Commented Aug 19, 2021 at 14:18

2 Answers 2

2

You can use parentheses like this:

({ id: string; answer: string | boolean;  } | { id: string; answers: { id: string }[] })[];
Sign up to request clarification or add additional context in comments.

3 Comments

WOOHOO!! Thanks!! P.s.: I have to wait 9 minutes before I can accept your answer
I would also recommend adding interfaces or type aliases, this is unwieldy.
@PaulB I believe you've waited more than 9 minutes 😄 anyways, I'm glad it helped you're l you. And as H.B. said, it's a good idea to break it down into type aliases or interfaces
0

I would suggest you to change the input. It would be hard to work with different variants of 'answer'/'answer'.

You can make something like that:

type Answer = string | boolean;
interface TestItem {
    id: string;
    answer?: Answer | Array<TestItem>;
}

const results: TestItem[] = [
        {
            "id": "test",
            "answer": "1"
        },
        {
            "id": "test_multi",
            "answer": [
                {
                    "id": "skill_1"
                },
                {
                    "id": "skill_2"
                }
            ]
        },
]

If you want to stay with 'answers', then try this:

type Answer = string | boolean;
interface TestItem {
    id: string;
    answer?: Answer;
    answers?: Array<TestItem>;
}

const results: TestItem[] = [
        {
            "id": "test",
            "answer": "1"
        },
        {
            "id": "test_multi",
            "answers": [
                {
                    "id": "skill_1"
                },
                {
                    "id": "skill_2"
                }
            ]
        },
]

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.