[英]Property 'map' does not exist on type '() => IterableIterator<number>'
I'm trying to pass keys
as a React prop:我正在尝试将
keys
作为 React 道具传递:
import * as React from "react";
import { render } from "react-dom";
const keys: string[] = ["a", "b"];
function App({keys}: string[]) {
return (
<div>
{keys.map((key: string) => (
<li>{key}</li>
))}
</div>
);
}
const rootElement = document.getElementById("root");
render(<App keys={keys} />, rootElement);
However, I get these errors:但是,我收到以下错误:
Property 'map' does not exist on type '() => IterableIterator'.
类型“() => IterableIterator”上不存在属性“map”。
Type 'string[]' is not assignable to type '() => IterableIterator'.
类型“string[]”不可分配给类型“() => IterableIterator”。
Why is this, and how to fix it?为什么会这样,如何解决?
Live code: https://codesandbox.io/s/changing-props-on-react-root-component-forked-9lj9ye?file=/src/index.tsx:0-341直播代码: https://codesandbox.io/s/changing-props-on-react-root-component-forked-9lj9ye?file=/src/index.tsx:0-341
You're typing your whole props as string[]
instead of just keys
.您将整个道具输入为
string[]
而不仅仅是keys
。 Embed keys
prop in an object:在 object 中嵌入
keys
道具:
function App({ keys }: { keys: string[]; }) {
// ...
Or using an interface:或者使用接口:
interface AppProps {
keys: string[];
}
function App({ keys }: AppProps) {
// ...
Or a type:或者一个类型:
type AppProps {
keys: string[];
}
function App({ keys }: AppProps) {
// ...
To go further: Differences Between Type Aliases and Interfaces进一步到 go: 类型别名和接口之间的区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.