javascript - 在类型 '{}'.ts(7053) 上找不到具有类型参数的索引签名
问题描述
我是反应打字稿的新手,这里我有道具及其类型,除了这部分之外一切都很好:collapseStates["" + el.name + el.identifier] = true; 它给出错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}”。在类型 '{}'.ts(7053) 上找不到具有类型参数的索引签名
将:collapseStates = {} 添加到接口 AA 的正确方法是什么?如您所见,它应该包含字符串collapseStates["" + el.name + el.identifier]
任何帮助或建议表示赞赏
interface AA{
action: "sites" | "analysers";
actionArgs?: string;
name: string;
identifier: string;
}
const Article: React.FC<AA> = (props) => {
let [state, setState] = useState<AA>({
actionArgs: props.actionArgs,
collapseStates: {} ,
});
const handleExpandClick = (event, key) => {
setState({
collapseStates: {
...state.collapseStates,
[key]: !state.collapseStates[key],
},
});
};
const initialize = () => {
let collapseStates = {};
articles.map((el: AA) => {
collapseStates["" + el.name + el.identifier] = true;
return;
});
setState({
...state,
collapseStates: collapseStates,
});
};
}
解决方案
您可以键入collapseStates
:Record
interface AA{
...
collapseStates: Record<string,boolean>;
}
推荐阅读
- .net - .Net 具有十六进制地址的多行正则表达式
- docker - docker-compose down 和 docker-compose kill 有什么区别?
- javascript - 元素未在反应状态上添加数组
- amazon-web-services - 如何让 AWS 作为 OIDC 或 SAML 提供商的 IdP 工作?
- python - 如何使用 fig.canvas.draw() 制作动画?
- javascript - 解决这个代码战挑战的更短更有效的方法?
- c# - WPF .NET 应用程序无法连接到不同网络上的 SQL Server
- ubuntu - 以其他用户身份运行 Jenkins - 无法在启动时运行
- java - Liquibase 使用 Postgis“几何”类型创建表
- javascript - 你怎么能把它变成一个dropup?