我使用打字稿并做出反应。
使用项目循环 ISech 的 sampleQuery、dammyQuery、foodQuery 和 groupQuery。
我在 save[i].query 收到以下错误。
错误
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ISearch'.
No index signature with a parameter of type 'string' was found on type 'ISearch'.
interface ISearch {
test: string;
sampleQuery: { id: number; query: string };
dammyQuery: { id: number; query: string };
foodQuery: { id: number; query: string };
groupQuery: { id: number; query: string };
}
const searchText = (save: ISearch) => {
const items = ['sampleQuery', 'dammyQuery', 'foodQuery', 'groupQuery'];
let text = '';
items.map((i) => {
if (save[i].query != '') {
text = text.concat(save[i].query + ',');
}
});
return text;
};
我认为有一种更优雅的方法可以做到这一点,但这是我的解决方案:
items.map((i) => {
let key = i as keyof ISearch
if (save[key].query != '') {
text = text.concat(save[key].query + ',');
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句