I'm trying to type an arrow function that gets a json file. The problem is that json returns me an object of objects.
I'm trying to make it so that I don't have to describe each object (because there may be several levels of nesting) and do it through the type:
type Dictionary = {
menu: Record<string, unknown>;
pages: Record<string, unknown>;
result: Record<string, unknown>;
};
My function:
const getData = async (lang: string): Promise<Dictionary> => {
const path = `src/assets/file${lang}.json`;
try {
const response = await fetch(path);
if (!response.ok) {
throw new Error(`Some error`);
}
const data = await response.json();
setDict(data.menu);
} catch (error) {
if (error) setError(error);
}
};
useEffect(() => {
getData(lang);
}, []);
But I get errors:
Unsafe assignment of an any
value.
Unsafe argument of type any
assigned to a parameter of type SetStateAction<null>
Unsafe member access .menu on an any
value.
Argument of type '{}' is not assignable to parameter of type 'SetStateAction'.
Type '{}' provides no match for the signature '(prevState: null): null'.
Where to start to fix it? What did I missed?