Ts(2322)导入时我的道具类型出错

时间:2019-10-10 01:33:53

标签: reactjs typescript

有人可以帮助我吗?我在Create React App中使用TypeScript,StyledComponent和React。该错误在构建期间发生,无法消除。

public class BasicClass
{
    public string StringProperty { get; set; }
    public int IntProperty { get; set; }
    internal string InternalStringProperty { get; set; }
}
// browser and terminal error

TypeScript error in ./src/index.tsx(4,1):
Type '{ alt: string; src: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'alt' does not exist on type 'IntrinsicAttributes'.  TS2322
// ./src/components/Image/index.tsx

import React from 'react'
import Styled from 'styled-components/macro'

interface ImageInterface {
  alt?: string | undefined
  src: string
}

const Image = Styled.img`
  max-width: 100%;
`

export default () => ({ alt, src }: ImageInterface) => (
  <Image alt={alt || 'My alternative text'} src={src} />
)
// ./src/index.tsx

import React from 'react'
import Image from './components/Image'

<Image src="https://cjpatoilo.com/initify/artwork.png" />

希望普通的Image编译组件没有错误

1 个答案:

答案 0 :(得分:0)

问题很可能出现在您的./src/components/Image/index.tsx文件中。默认导出是返回组件的函数。但是它在./src/index.tsx中使用,就好像它是一个功能组件一样。

第一个选择是删除此包装功能:

// ./src/components/Image/index.tsx

export default ({ alt, src }: ImageInterface) => (
  <Image alt={alt || 'My alternative text'} src={src} />
)

或者,调用此方法,然后将返回值用作组件。

// ./src/index.tsx

import Fn from './components/Image'

const Image = Fn();

<Image src="https://cjpatoilo.com/initify/artwork.png" />