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
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
Looks like you are trying to get the property from an array.
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?Try putting [number]
tried that
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'.
Cause it could be undefined?
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)