正如错误所述,
null
和
undefined
无法分配给未写入以接受它们的类型。因此,您有两种解决方案:
// 1. My personal recommendation, define an initial value for creditData
this.state = {
loading: false,
query: '',
creditData: { description: '', rating: 0 }
}
// 2. Make creditData optional in your interface (and initialize with undefined instead of null)
// This is useful if you want to differentiate states
// between whether or not you have the data, or if there is no useful default value.
interface CompanySearchState {
loading: boolean;
query: string;
creditData?: CreditData
}