[TypeScript] How would you go about creating an Array that can have different types of objects?
Firstly sorry if this isn't the right place to ask TS question, but I didn't know where else to ask 😢
I have a dilemma in a project I am working on. The example I will give is not how we do it, but you should get the idea... The project is about task solving and because of some cases we have decided to have a type like so:
Where the tasks array holds all the user's tasks. But like you can see anyone of these elements in the array can be of a completely different type. What I want to do is render a different component based on what type of object it is, so I do this:
However, since it doesn't know what type it is, I don't get type safety. How can I check which type it is? Is that even possible?
Also, keep in mind that in the future we might add more task types so I have to take that into account as well.
15 Replies
[EDIT] I have tried using typeof, however i cannot compare object types only primitives. Except if I do something like this, but it seems to complicated for this use case:
or this, which seems even more weird:
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Nice, Thank you!
But does this only work if I have a unique property in a type?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Some of them match, for example we have: name, maxPoints, points... However some are called the same but have different types, for example: results. Some are just string, some are Array<string>, or Array<SomeStructure>
just give them a shared key like
tasktype: 'something' | 'somethingElse'
then you can do a discriminated union on themUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
I see now, if they all have the same prop TS is smart enough to give type safety for just that one...
Handbook - Unions and Intersection Types
How to use unions and intersection types in TypeScript
Tyvm both of you, you solved it for me 💪
read through this docs page
theres a lot of good stuff in it
Doesn't matter that it's deprecated?
oh my bad
theres a link to the new version
in the thing that says that its deprecated
i think this page didnt change though
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
the reason, btw, that you have to add (or use an existing) a discriminating property is that typescript is compile time ONLY
so just because typescript knows at compile time that it added some numbers, strings, Circles, Squares, whatever, to an array
there’s no way for it to know what’s what when looping unless there’s something concretely different about the elements, something you can test for with JS