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 Lind17mo 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 Lind17mo ago
e of pi
e of pi17mo ago
Looks like you are trying to get the property from an array.
Christian Lind
Christian Lind17mo 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
bakdaddy17mo ago
Try putting [number]
Christian Lind
Christian Lind17mo ago
tried that
Christian Lind
Christian Lind17mo ago
Christian Lind
Christian Lind17mo 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
bakdaddy17mo ago
Cause it could be undefined?
e of pi
e of pi17mo 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