How to fix the Type issue in react typescript.
I am new to react-typescript, and I am fetching objects with string property from the server. And updating the state.
How to fix the error with type:
Argument of type 'string' is not assignable to parameter of type 'never'
formMethods and setValue coming from the react-hook-form
useEffect(() => {
if (address.postalCode?.length === 5) {
const getCityState = async () => {
const response = await getUspsCityState(address.postalCode);
if (response) {
const { city, state } = response;
// HERE I AM getting error:
// Argument of type 'string' is not assignable to parameter of type 'never'
formMethods.setValue('address.city', city, {
shouldDirty: true,
shouldValidate: true,
});
}
};
getCityState();
}
}, [formMethods, watchPostalCode]);
This is getUspsCityState function:
export const getUspsCityState = (
zipcode: string,
): Promise<UspsCityState | null> =>
new Promise((resolve, reject) => {
try {
const xmlString = `<?xml version="1.0"?><CityStateLookupRequest USERID="${USPS_USER_ID}"><ZipCode><Zip5>${zipcode}</Zip5></ZipCode></CityStateLookupRequest>`;
const api = 'CityStateLookup';
return fetch(`${USPS_SERVER}?API=${api}&XML=${xmlString}`, {
method: 'GET',
})
.then(response => response.text())
.then(xmlResponse => {
const state = xmlResponse.match(/<State>([^<]+)<\/State>/i);
const city = xmlResponse.match(/<City>([^<]+)<\/City>/i);
if (state && state[1] && city && city[1]) {
return resolve({ state: state[1], city: city[1] });
}
return resolve(null);
});
} catch (e) {
return reject(e);
}
});