So I have object in typescript used in react components that has this type:
export type DataInfoTypes<dataType> = {
data: dataType | null;
loading: boolean;
error: boolean;
};
Let's assume that "dataType" there would be (should not matter, just an object of strings):
{[key in string]: string}
Then when I use this data in React component, I check if object.data is not null:
if (!materials.data || materials.loading) {
return <DataLoading />;
}
Then in my return function, when I reference data it seems okay:
<Flex alignItems="center" direction="column">
<Text>{materials.data["somekey"]}</Text>
<VStack w="100%" ....
However few lines down I have an array map method(in same component, same return):
<VStack w="100%" align="center">
{sources.map((matKey: string, index: number) => {
return <Text key={index}>{materials.data["somekey"]}</Text>
}
</VStack>
And I get this error:
Object is possibly 'null'. TS2531
Any ideas on how to fix this or what is the cause?
(Please exclude all the tsignore solutions if possible)
Big thanks to everyone responding.