18

TypeScript: 2.8.3
@types/react: 16.3.14


The type of return in function component is JSX.Element, when I declare the component to React.SFC(alias of React.StatelessComponent).

There are three errors occured:

  1. TS2322: Type 'Element' is not assignable to type 'StatelessComponent<{}>', Type 'Element' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any>'

  2. TS2339: Property 'propTypes' does not exist on type '(props: LayoutProps) => StatelessComponent<{}>'

  3. 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']}
5
  • 16
    The return type is not a component, the function itself is component. const LayoutHeaderItem: React.SFC<LayoutHeaderItemProps> = (props: LayoutHeaderItemProps) => { ... } Commented May 21, 2018 at 5:41
  • @AlekseyL. Thank you for your answer ^_^. Commented May 21, 2018 at 6:26
  • You are welcome! Commented May 21, 2018 at 11:28
  • @AlekseyL. Is there a particular reason why a lot of people don't write answers as answers but as a comment? I'm really curious because I see that a lot on stackoverflow Commented May 8, 2019 at 15:34
  • 1
    @Truntle no particular reason. Sometimes questions need clarification. Posted as an answer :) Commented May 8, 2019 at 16:03

1 Answer 1

17

The return type is not a component, the function itself is a component:

const LayoutHeaderItem: React.SFC<LayoutHeaderItemProps> =
    (props: LayoutHeaderItemProps) => { 
        // ... 
    }

This question is a bit old, SFC deprecated in favor of FunctionComponent with a FC alias

const LayoutHeaderItem: React.FC<LayoutHeaderItemProps> =
    (props: LayoutHeaderItemProps) => { 
        // ... 
    }
Sign up to request clarification or add additional context in comments.

2 Comments

In order to use React.StatelessComponent must ES6 Arrow Function Syntax be used? I was hoping to use function() {} function declaration syntax too.
@MichaelR you can't use it with function as is. Relevant suggestion here. If you really have to use a function you'll need to type parameters and return type separately (Parameters and ReturnType helpers can be used)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.