typescript:2.8.3
@types/react:16.3.14
JSX.Element
当我将组件声明为React.SFC
(的别名 React.StatelessComponent
)时,函数组件中返回的类型是。
出现了三个错误:
TS2322: Type 'Element' is not assignable to type 'StatelessComponent<{}>', Type 'Element' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any>'
TS2339: Property 'propTypes' does not exist on type '(props: LayoutProps) => StatelessComponent<{}>'
TS2339: Property 'defaultProps' does not exist on type '(props: LayoutProps) => StatelessComponent<{}>'
interface ItemInterface {
name: string,
href: string,
i18n?: string[]
}
interface LayoutHeaderItemProps extends ItemInterface{
lang: string,
activeHref: string,
}
function LayoutHeaderItem (props: LayoutHeaderItemProps): React.SFC{
const {name, href, lang, activeHref, i18n} = props
const hrefLang = /\//.test(href) ? `/${lang}` : ''
if (!i18n.includes(lang)) return null
return (
<a
className={`item${href === activeHref ? ' active' : ''}`}
key={href}
href={hrefLang + href}
><span>{name}</span></a>
)
}
LayoutHeaderItem.propTypes = {
lang: string,
activeHref: string,
name: string,
href: string,
i18n: array
}
LayoutHeaderItem.defaultProps = {i18n: ['cn', 'en']}