[英]Property 'map' does not exist on type '() => IterableIterator<number>'
我正在嘗試將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);
但是,我收到以下錯誤:
類型“() => IterableIterator”上不存在屬性“map”。
類型“string[]”不可分配給類型“() => IterableIterator”。
為什么會這樣,如何解決?
您將整個道具輸入為string[]
而不僅僅是keys
。 在 object 中嵌入keys
道具:
function App({ keys }: { keys: string[]; }) {
// ...
或者使用接口:
interface AppProps {
keys: string[];
}
function App({ keys }: AppProps) {
// ...
或者一個類型:
type AppProps {
keys: string[];
}
function App({ keys }: AppProps) {
// ...
進一步到 go: 類型別名和接口之間的區別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.