Weird typescript error

Hi all, im getting a typescript error i dont understand why im getting. As you can see selectedQuestion is assigned the type that you can see, ie a object called SeenQuestions along with some type extensions. The type SeenQuestion absolutley has a property called .questionType - so why am i getting this error? Help greatly appreciated
10 Replies
Christian Lind
Christian LindOP2y ago
So to simplfy, im getting an error when trying to access .questionType, even though it exists on the object Here is the error that im getting
Christian Lind
Christian LindOP2y ago
e of pi
e of pi2y ago
Looks like you are trying to get the property from an array.
Christian Lind
Christian LindOP2y ago
hmm yes you are correct but im setting the type like this, (typeof savedQuestions.data)[] | null shouldn't the square brackets instead look at the elements inside of the array?
bakdaddy
bakdaddy2y ago
Try putting [number]
Christian Lind
Christian LindOP2y ago
tried that
Christian Lind
Christian LindOP2y ago
Christian Lind
Christian LindOP2y ago
got this error in the terminal also Type error: Type '(SeenQuestions & { relatedXyz: Xyz | null; relatedKva: Kva | null; relatedNog: Nog | null; relatedDtk: Dtk | null; relatedOrd: Ord | null; relatedLas: Las | null; relatedMek: Mek | null; })[] | undefined' has no matching index signature for type 'number'.
bakdaddy
bakdaddy2y ago
Cause it could be undefined?
e of pi
e of pi2y ago
If you want to get questionType for each element in the array, try using map. If you expect to have a single element then something is wrong and nobody can tell without the full code (try a link on typescript playground)
Want results from more Discord servers?
Add your server