microsoft / microsoft/TypeScript

Support non-constructor JSX factory functions

オープン
#37,733 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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:
https://github.com/microsoft/TypeScript/issues/5740
https://github.com/microsoft/TypeScript/issues/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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供されたJSX再現コードから始め、一覧にあるTypeScriptのバージョン間でその動作を比較します。JSX class components、factory functions、function componentsがどのように区別されるかを追跡し、validなfactory functionsとinvalidなfactory functionsが期待されるdiagnosticsを受け取ることを示すカバレッジを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。