Sorry for the wierd title...theres a few layers to this one that made it hard to summarise into a title haha.
I have made two functions to retreive data from AsyncStorage:
export const getItem = async (key: string) => { try { const value = await AsyncStorage.getItem(key); return value; } catch { console.log('Async Storage: Something went wrong getting '+ key); }}export const getParsedItem = (key: string) => { const jsonValue = getItem(key); return jsonValue != null ? JSON.parse(jsonValue) : null}
This is all fine and well but in the second function I get a tpyescript error on jsonValue
inside the JSON.parse: Argument of type 'Promise<string | null | undefined>' is not assignable to parameter of type 'string'.
I thought the getItem function os AsyncStorage should be returning a string?
What am I doing wrong here?
In another part of my application I have this:
let testArr = await AsyncStorage.getItem('test-arr');testArr = testArr && JSON.parse(testArr);testArr.forEach(item => { ... });
If I console.log(testArr)
then it is an array...the variable has what I need but on the forEach I get the following typescript error: Property 'forEach' does not exist on type 'string'.
I think it traces back to how I am getting it from AsyncStorage but I cant set the type anywhere along the process or it complains that it isnt a string of null. How do I set the type to an array once I have retreived the data from AsyncStorage and JSON.parse()
ed.