Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy produces TypeScript error: JSX element type 'Component' does not have any construct or call signatures #3219

Closed
1 task
birtles opened this issue Jul 6, 2021 · 3 comments · Fixed by #4547
Labels

Comments

@birtles
Copy link

birtles commented Jul 6, 2021

  • Check if updating to the latest Preact version resolves the issue (it does not -- 10.5.13 is fine, but 10.5.14 is broken for me)

Describe the bug

As of 10.5.14 code like the following:

const MyComponent = lazy(() =>
  import('./my-component').then((m) => {
    return {
      default: m.MyComponent,
    };
  })
);

may begin to error with: TS2604: JSX element type 'MyComponent' does not have any construct or call signatures

#3139 changed the type of lazy as follows:

- export function lazy<T>(loader: () => Promise<{ default: T }>): T;
+ export function lazy<T>(loader: () => Promise<{ default: T } | T>): T;

From what I can tell, in the above code fragment, TypeScript tries to infer the type of T by matching the return value of the function against the newly added T instead of { default: T } and hence resolves the return type of lazy as { default: React.FunctionComponent<Props> } not React.FunctionComponent<Props> as it previously did.

For the time being it seems possible to work around this by explicitly providing the generic type parameter such as follows:

import type { Props as MyComponentProps } from './my-component';

const MyComponent = lazy<React.FunctionComponent<MyComponentProps>>(() =>
  import('./my-component').then((m) => {
    return {
      default: m.MyComponent,
    };
  })
);
@JoviDeCroock
Copy link
Member

This has been implemented in #3139

@birtles
Copy link
Author

birtles commented Nov 15, 2021

This has been implemented in #3139

Is that right? #3139 is the regressing issue, not the fix.

@JoviDeCroock JoviDeCroock reopened this Nov 15, 2021
@JoviDeCroock
Copy link
Member

You are completely right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants