简体   繁体   English

属性 'map' 在类型 '() => IterableIterator 上不存在<number> '</number>

[英]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.

相关问题 属性“地图”在类型“对象”角度上不存在 - Property 'map' does not exist on type 'Object' Angular 离子2:“属性&#39;号&#39;&#39;在类型&#39;typeof上不存在” - Ionic 2 : “Property 'number' does not exist on type 'typeof” 类型 'number | 上不存在属性 'getTime' 日期' - Property 'getTime' does not exist on type 'number | Date' 类型“NumberSchema”上不存在属性“长度”<number></number> - Property 'length' does not exist on type 'NumberSchema<number> Ionic&AngularFireDatabase列表结果-类型“ {}”上不存在属性“ map” - Ionic & AngularFireDatabase list result - Property 'map' does not exist on type '{}' React typescript 属性“地图”在“用户”类型上不存在 - React typescript Property 'map' does not exist on type 'User' Angular 6:“Observable”类型上不存在“map”属性<Response> &#39; - Angular 6: Property 'map' does not exist on type 'Observable<Response>' “AxiosResponse”类型上不存在属性“地图”<any> '.ts(2339)</any> - Property 'map' does not exist on type 'AxiosResponse<any>'.ts(2339) React &amp; Typescript:“数字”类型上不存在属性“id” - React & Typescript: Property 'id' does not exist on type 'number' React js 中的类型 'Record<"id", string>' 上不存在属性 'number' - Property 'number' does not exist on type 'Record<"id", string>' in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM