[英]Binding element 'string' implicitly has an 'any' type
我有这样的代码:
function Child( { name: string } ) {
return (
<div>
{name ? (
<h3>
The <code>name</code> in the query string is "{name}
"
</h3>
) : (
<h3>There is no name in the query string</h3>
)}
</div>
);
}
我得到了:
我试图以多种方式传递名称name
文字,但 TypeScript 总是很复杂,知道如何让 TypeScript 开心吗?
function Child(name: { name: String}) { ...
/// ^ this is not typing the props,
/// it's typing the first parameter called name
要正确键入 React 道具,您必须这样做。
function Child({name}: {name: string}) { ...
您可能还想使用类型string
而不是包装String
,因为它们不是一回事。 建议尽可能使用string
。 查看此问题以获取更多信息。
同样根据错误, query.get("name")
function 正在返回一个可为空的string
。 您可能想在使用它之前对其进行转换,以确保它不是 null。
<Child name={query.get("name") as string} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.