【发布时间】:2019-03-24 04:24:09
【问题描述】:
这是我的一段代码:
type Data = {
data: Response;
}
type Response = {
data: Cards[];
}
type Cards = {
name: string;
}
class Editor extends Component<Props> {
componentDidMount() {
const { cardUnderEdition } = this.props;
const description = cardUnderEdition && cardUnderEdition.currentCard.description;
const currency = cardUnderEdition && cardUnderEdition.currentCard.name;
fetch('https://api.coinmarketcap.com/v2/ticker') //eslint-disable-line
.then(res => res.json())
.then((data: Data) => this.setState({
data: Object.values(data.data),
description: cardUnderEdition === undefined
? ''
: description,
currentCurrency: cardUnderEdition === undefined
? Object.values(data.data)[0].name
: currency,
}));
}
我在Object.values(data.data)[0].name 中有一个错误Property name does not exist on type {}。
添加“名称”属性类型检查的适当方法是什么?
建议更改后,我收到Property name does not exist on type Cards[]
【问题讨论】:
-
如果你想给一个对象添加一个属性,你需要这样做 obj['name']='a name'
-
这不是问题。 Tslint 看到 Object 没有属性“name”并想要这样的东西:{ name: string }
标签: reactjs typescript tslint