reactjstypescriptspfxtypescript-2.5

TypeScript error will not satisfy


I'm using TypeScript 2.5.2. I have tried downgrading to TypeScript 2.2.2, which the same code has worked on another project.

**Error: file: Weather.tsx' severity: 'Error' message: 'Argument of type '(response: IListItem) => void' is not assignable to parameter of type '(value: { weather: IListItem; }) => void | PromiseLike'. Types of parameters 'response' and 'value' are incompatible.

Type '{ weather: IListItem; }' has no properties in common with type 'IListItem'.'**

What is wrong with the syntax?

Source code on GitHub

VSCode Screenshot


Solution

  • From just looking at these types and not the rest of the code: the type parameter in .then((response: HttpClientResponse): Promise<{weather: IListItem}> specifies that the response is an object with a weather property of type IListItem rather than the bare IListItem you expect at .then((response: IListItem): void => {, which explains the type error.

    If the api response is indeed an object, you can change the latter to .then((response: {weather: IListItem}) and add .weather to each appearance of response in the body (or even simply destructure without a type annotation: then(({weather}): void => { and use weather instead of response.)

    On the other hand, if the api just returns an IListItem, you can change the former to .then((response: HttpClientResponse): Promise<IListItem> => {.