I have a project in react native using typescript and I would like to use Promise.resolve().then() of an object with a condition which causing a type error in typescript. Below is the situation:
I have the first object (as sample):
const objectA = async () => {
// get the object from local storage
const user = await asyncStorage.getItem(userAKey)
return {
// types are all in string
id: user?.id || '',
email: user?.email || '',
}
}
And I have my second object (as sample):
const objectB = async () => {
// get the object from local storage
const user = await asyncStorage.getItem(userBKey)
return {
// types are all in string
id: user?.id || '',
email: user?.email || '',
organisation: user?.organisation || ''
}
}
So both objects have different types and keys with value. Here is the problem, I have a condition to check which object to be resolve based on some host type:
const objectTobeResolved = type === 'primary' ? objectA : objectB
So based on that condition, I pass it to the Promise:
const userData = () => {
Promise.resolve(objectTobeResolved).then((values) => { ... })
}
I will get the type error here in the typescript for objectTobeResolved.
Basically the type error here is mentioning something like objectA | objectB, but I actually only need to pass one of the object based on the condition above and I don't need the objectA | objectB condition as it is only either objectA or objectB based on the declared host type I had in the app launcher.
I don't quite understand how to declare it properly in typescript since I have just learned to use Typescript.
What is a right way to write the promise here? should I use race or all instead? But I only need to check one object at a time. Or should I declare or initialise in a certain way? Or maybe I don't even need to use promise at all?
Edit: I have updated the codes above to my real case scenario. The codes are actually running correctly, just that the type error keep showing up and I couldn't pass the test. A shortcut is to use //@ts-ignore, but since our practice is to not ignore the warning, so I'm trying to find a correct way to write it without having a warning which causing the test to be failed.