Версия машинописного текста: 2.8.3, рассмотрите следующий фрагмент кода
import axios from "axios";
import { Component } from "react";
import * as React from "react";
interface ICustomer {
id: number
firstName: string
lastName: string
}
interface IState {
customers: ICustomer[]
}
class AllCustomers extends Component<{}, IState> {
public state = {
customers: []
}
public componentDidMount() {
axios.get<ICustomer[]>(`http://localhost:8080/customers`)
.then(resp => this.setState({customers: resp.data}))
}
public render() {
const {customers} = this.state;
return (
<table>
{
customers.map(customer => (
<tr key={customer.id}>
<td>{customer.firstName}</td>
<td>{customer.lastName}</td>
</tr>
))
}
</table>
)
}
}
Я получаю ошибки времени компиляции, такие как customer.id
не является полем типа never
Почему-то ... тип this.state.customer
подразумевается как never[]
, это просто неправильно. Как пустой массив в качестве начального значения не является допустимым экземпляром присваиваемого типа массива?