Home > OS >  Argument of type 'unknown' is not assignable to parameter of type
Argument of type 'unknown' is not assignable to parameter of type

Time:10-14

data

Argument of type 'unknown' is not assignable to parameter of type 'SetStateAction<IAluno[]>'.
  Type 'unknown' is not assignable to type '(prevState: IAluno[]) => IAluno[]'.  TS2345

    23 |         const response = await api.get('/alunos');
    24 |         console.log(response);
  > 25 |         setAlunos(response.data)
       |                   ^
    26 |         
    27 | 
    28 |     }

I'm lost, I can understand where this 'unknown' type came from...

I'm trying to collect some info from an old api that returns a json.

code

CodePudding user response:

TS does not know what response might contain and if data is even a property in response. It complains about the type of response.data.

If you are confident of the response you can use casting using as keyword.

setAlunos(response.data as IAluno[])

CodePudding user response:

It looks like the response does not have "data" property. Probably it is called differently. Try to console.log(response) and look for the property you are looking for in this object

CodePudding user response:

It looks like the unknown type is the response.data that you are trying to pass in as a parameter at

setAlunos(response.data)

I would double-check that response.data is what you are expecting or that it exists. For example, does response?.data work?

  • Related