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

Support non-constructor JSX factory functions #37733

Open
use-strict opened this issue Apr 1, 2020 · 2 comments
Open

Support non-constructor JSX factory functions #37733

use-strict opened this issue Apr 1, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@use-strict
Copy link

TypeScript Version: 3.3.3 to 3.8.3

Search Terms:
jsx factory function constructor component ElementClass

Expected behavior:
I'm trying to pass a factory function instead of a class component as a JSX element constructor function. It should be possible according to the Handbook, as described here. See the example under the Type checking -> class component section (I can't deep-link with hash/anchor because the server isn't properly configured :( ). I should get no errors with the code provided below.

Actual behavior:
I get the following error:

JSX element type '{ render: () => { custom: string; }; }' is not a constructor function for JSX elements.
  Property 'custom' is missing in type '{ render: () => { custom: string; }; }' but required in type 'Element'.(2605)

From what I can tell, this used to work, but the behavior broke somewhere between v3.1.6 and v3.3.3 (as tried in the Playground). I'm guessing there was some change that altered detection between React function components and class components and that change introduced this bug. I'm not even sure if React runtime even allows factory functions, but my use case is a custom library and I'm not using React at all. This error is the same regardless of using React or not.

Also, with the exact example from the Handbook, because the JSX.Element is not specified explicitly, there is no error, even when there should be one. See Playground Link for Handbook example

Related Issues:
#5740
#5478

Code

declare namespace JSX {
  interface Element {
    custom: string;
  }
  interface ElementClass {
    render: () => Element;
  }
}

class MyComponent {
  render() {
    return {
      custom: "2"
    };
  }
}
function MyFactoryFunction() {
  return { render: () => { return { custom: "2" }; } }
}
function MyFunctionComponent() {
  return { custom: "2" }
}

<MyComponent />; // ok
<MyFactoryFunction />; // ok
<MyFunctionComponent />; // ok

class NotAValidComponent {}
function NotAValidFactoryFunction() {
  return {};
}

<NotAValidComponent />; // error
<NotAValidFactoryFunction />; // error
Output
"use strict";
class MyComponent {
    render() {
        return {
            custom: "2"
        };
    }
}
function MyFactoryFunction() {
    return { render: () => { return { custom: "2" }; } };
}
function MyFunctionComponent() {
    return { custom: "2" };
}
<MyComponent />; // ok
<MyFactoryFunction />; // ok
<MyFunctionComponent />; // ok
class NotAValidComponent {
}
function NotAValidFactoryFunction() {
    return {};
}
<NotAValidComponent />; // error
<NotAValidFactoryFunction />; // error
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 1, 2020
@RyanCavanaugh RyanCavanaugh changed the title JSX component factory functions don't work Support non-constructor JSX factory functions Apr 1, 2020
@use-strict
Copy link
Author

@RyanCavanaugh , I see that you marked this as a Suggestion. I believe this is a bug, because the feature is documented in the official handbook, as described above, and it used to work in previous versions.

I know it's still an obscure case, but if this functionality was dropped on purpose (please confirm?), then my issue should be closed and I can open a PR to remove the misleading documentation from the handbook repo.

P.S. Sorry for tagging you, I know the repo is assaulted with new issues every day and there is limited time for triage.

@robbiespeed
Copy link

robbiespeed commented Apr 18, 2023

5.1 should provide a workaround, here's a playground using the nightly build. JSX.ElementType was added in #51328.

The class components section in the handbook definitly needs updating though because it does state that JSX factory functions work using JSX.ElementClass. Potentially JSX.ElementClass can and should be deprecated now that JSX.ElementType exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants