简体   繁体   English

React Typescript:类型“X”不可分配给类型“IntrinsicAttributes & {properties}”。 TS2322

[英]React Typescript: Type 'X' is not assignable to type 'IntrinsicAttributes & { properties }. TS2322

I'm trying to pass a prop to a menu item component but I get this error with the imageURL attribute:我正在尝试将道具传递给菜单项组件,但使用 imageURL 属性出现此错误:

Type '{ key: number; title: string; imageURL: string; }' is not assignable to type 'IntrinsicAttributes & { title: string; }'.
Property 'imageURL' does not exist on type 'IntrinsicAttributes & { title: string; }'.  TS2322

It looks like there is a type mismatch for imageURL but I can't seem to find the solution.看起来 imageURL 的类型不匹配,但我似乎找不到解决方案。 Here are the relevant components:以下是相关组件:

MenuItem.tsx菜单项.tsx

import React from 'react';
import './MenuItem.scss'

const MenuItem = ({ title }:{ title:string }) => (
    <div className='menu-item'>
        <div className='content'>
            <h1 className='title'>{title}</h1>
            <span className='subtitle'>SHOP NOW</span>
        </div>
    </div>
);

export default MenuItem;

Directory.tsx目录.tsx

import React from 'react';
import './Directory.scss';
import MenuItem from '../menuItem/MenuItem';

type MenuItems = {
    title:string;
    imageURL:string;
    id:number
};

class Directory extends React.Component<Object, any> {
    constructor(props:object) {
        super(props);

        this.state = {
            sections: [
                {
                  title: 'category1',
                  imageURL: 'https://website1.com',
                  id: 1
                },
                {
                  title: 'category2',
                  imageURL: 'https://website2.com',
                  id: 2
                },
                {
                  ...
                }
              ]
        };
    };

    render() {
        return (
            <div className='directory-menu'>
                {
                    this.state.sections.map(({ title, imageURL, id }:MenuItems) => (
                        <MenuItem key={id} title={title} imageURL={imageURL} />
                    ))
                }
            </div>
        );
    };
};

export default Directory;

You have just ({ title }:{ title:string }) as the props passed in for you're MenuItem component.您只有({ title }:{ title:string })作为传入的道具,您是 MenuItem 组件。 You need the props to match the type you're using in the component.您需要道具与您在组件中使用的类型相匹配。

This这个

const MenuItem = ({ title }:{ title:string }) => (
  <div className='menu-item'>
    <div className='content'>
        <h1 className='title'>{title}</h1>
        <span className='subtitle'>SHOP NOW</span>
    </div>
  </div>
);

Should be应该

const MenuItem = (title,imageURL}:{title:string;mageURL:string}) => (
 <div className='menu-item'>
    <div className='content'>
        <h1 className='title'>{title}</h1>
        <span className='subtitle'>SHOP NOW</span>
    </div>
  </div>
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String' TS React 数据导入:“TS2322:类型 &#39;{ data: { key1: string; }; }&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; Props&#39;。” - TS React data import: "TS2322: Type '{ data: { key1: string; }; }' is not assignable to type 'IntrinsicAttributes & Props'. " React.js和打字稿错误TS2322:无法将类型分配给&#39;IntrinsicAttributes和IntrinsicClassAttributes类型 - Reactjs and typescript error TS2322: Type is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes 非常简单的 MWE,typescript 错误 TS2322 (...) 不能分配给类型 'IntrinsicAttributes &amp; Props &amp; { children?: ReactNode; }' - Very simple MWE, typescript error TS2322 (…) not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }' 类型“IntersectionObserver”不可分配给类型“null”。 TS2322 [React,intersectionObserver] - Type 'IntersectionObserver' is not assignable to type 'null'. TS2322 [React, intersectionObserver] Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined' react-router-dom TypeScript TS2322:类型'typeof Index'不能分配给类型 - react-router-dom TypeScript TS2322: Type 'typeof Index' is not assignable to type 奇怪的 TS2322(类型不可分配)错误 - Strange TS2322 (Type not assignable) Error TS2322-类型X不能分配给类型Y,但是Y中的所有字段都包含在X中 - TS2322 - type X is not assignable to type Y, but all fields in Y are contained in X TypeScript 错误 TS2322:类型“选项”不可分配给类型“(字符串和选项)| (号码和选项)| (只读字符串 [] &amp; 选项)' - TypeScript Error TS2322: Type 'Option' is not assignable to type '(string & Option) | (number & Option) | (readonly string[] & Option)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM