Home > Software engineering >  Property 'name' does not exist on type 'never'.ts(2339)
Property 'name' does not exist on type 'never'.ts(2339)

Time:10-05

I'm using flight radar api to create a simple application. I store the results of the Api call in state like this

  const [airports, setAirports] = useState([]);

airports is an array of objects.

When I try to log out an airport name like this

airports.map(airport => console.log(airport.name));

I get an error in Vs that Property 'name' does not exist on type 'never'.

I'm new to Typescript I have no idea how to solve that.

CodePudding user response:

You have to explicitly tell the type of airports like this:

const [airports, setAirports] = useState<Airport[]>([]);

Right now in your code, typescript is inferring airports to be never[], hence the error.

  • Related