【发布时间】:2020-11-14 15:12:17
【问题描述】:
我有一个问题,我不明白它为什么这么说。
问题:
TS7053:元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型“typeof”
我有一个变量作为字符串。我们可以称它为componentName。我导入了一些这样的组件:“import * as _icons from "./icons";”我想邀请一个像“_icons[this.props.name!]”这样的组件。但是我的ide警告我作为错误句
import {SvgIconNamespace} from "../namespaces";
import * as _icons from "./icons";
import {AbstractIconComponent} from "./icons";
import React from "react";
import {View} from "react-native";
export class Icon extends AbstractIconComponent<SvgIconNamespace.SvgIconPropsInterface, SvgIconNamespace.SvgIconStateInterface> {
render(): React.ReactNode {
let iconElement = React.createElement(
_icons[this.props.name!], // Error sentence in this line!
{ size: this.props.size!, fill: this.props.fill!},
)
return(
<View>
{ iconElement }
</View>
);
}
constructor(props: SvgIconNamespace.SvgIconPropsInterface) {
super(props)
}
}
props.name: string = 'PlusSquareIcon'
【问题讨论】:
标签: reactjs typescript types native