question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript FunctionComponent

See original GitHub issue

Hi, maybe I’m missing something but here is the situation. I have a functional component with this signature:

const Input : FunctionComponent<Props> = (props: Props)

Then using Storybook for Preact I have the following story

storiesOf('Input', module)
    .add('Sample', () => <Input type="text" />)

And in this situation I get a typescript validation error with the following message at the story level. JSX element type 'VNode<{}>' is not a constructor function for JSX elements. Type 'VNode<{}>' is missing the following properties from type 'Element': nodeName, attributes, children

So I add method is expecting a RenderFunction which is an alias to one or more Preact.AnyComponent or JSX.Element.

Then I checked the FunctionComponent and it return a function that returns VNode or null.

So I checked and JSX.Element extends VNode<any> so this should be correct. But doesn’t work.

If on index.d.ts I changed the interface

interface FunctionComponent<P = {}> {
    (props: RenderableProps<P>, context?: any): VNode | null;
    displayName?: string;
    defaultProps?: Partial<P>;
}

To

interface FunctionComponent<P = {}> {
    (props: RenderableProps<P>, context?: any): preact.JSX.Element | null;
    displayName?: string;
    defaultProps?: Partial<P>;
}

Then the error gets solved but I’m not sure this is correct or if I’m doing something wrong. Btw I’m using the latest release of preact

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
gianpajcommented, Jul 16, 2020

For others arriving here for a similar issue.

This fails:

const Avatar = (props: IProps): FunctionalComponent<IProps> => {

  return (
    <span>
     hello
    </span>
  );
};
export default Avatar;

Error in the return:

Type 'Element' is not assignable to type 'FunctionalComponent<IProps>'.
  Type 'Element' provides no match for the signature '(props: Readonly<IProps & Attributes & { children?: ComponentChildren; ref?: RefObject<any> | RefCallback<any> | undefined; }>, context?: any): VNode<...> | null'.

and error when using this component:

'Avatar' cannot be used as a JSX component.
  Its return type 'FunctionalComponent<IProps>' is not a valid JSX element.ts(2786)

This is correct:

const Avatar: FunctionalComponent<IProps> = (props: IProps) => {

  return (
    <span>
     hello
    </span>
  );
};
export default Avatar;

the component should be a FunctionalComponent. It doesn’t return one

2reactions
ForsakenHarmonycommented, May 8, 2019

Have you configured jsxFactory?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function Components | React TypeScript Cheatsheets
FunctionComponent is explicit about the return type, while the normal function version is implicit (or else needs additional annotation). It provides ...
Read more >
TypeScript with React Functional Components
TypeScript is a superset of JavaScript that forces static typing and compiles into plain JavaScript. Similar to other statically-typed languages ...
Read more >
How to create Functional component in React Typescript ...
How to create Functional components and define props and pass data to component typed FunctionComponent or FC Type React Typescript with examples of ......
Read more >
TypeScript and React: Components - fettblog.eu
Functional components are my most favourite thing in React. They are simple, purely functional and super easy to reason about. The following shows...
Read more >
Defining Props in React Function Component with Typescript
This guide will provide you with the syntax you need to properly define the props entering your components, compare and contrast defining ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found