microsoft / microsoft/TypeScript

[Regression] Circular reference error when passing a class expression with non-primitive static properties to a generic function

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

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

Bug Domain: check: Type Circularity Help Wanted
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

class expression static type

🕗 Version & Regression Information
  • This changed between versions 4.0.5 and 4.1.5
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.1.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzg2RlQElUAHZDAHgBUA+ACi0uoC546BKTugbgBQgsHgDOGeFHgBeAiCIlyVDE0hQxY+AEF4Ab0EBICVGxgkOHLP0BfQTe5A

💻 Code
declare function returnInput<T>(input: T): T;

const a = returnInput(class A {
	static foo = {}
//              ^?
})
🙁 Actual behavior

A.foo: any
Error: 'foo' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)

And also note that this bug only happens when the type of foo is not a primitive type.

🙂 Expected behavior

A.foo: {}, as it works in 4.0.5

Additional information about the issue

To define a React class component, it is a common practice to include its "sub-components" as private static properties. I also use a "observer" function to make the state change auto observed. So the real-world code may look like:

import { observer } from 'mobx-react';
import { styled } from 'styled-components';

@observer
class ListView extends React.Component {
    // render method here...
    private static readonly Item = observer(class Item extends React.Component {
        private static readonly Header = styled.h4` // <- Cannot infer the type of Header
        // CSS style here...
        `;
    })
}

A workaround is to use static block to initialize the static property but it's cumbersome:

const a = returnInput(class A {
	static foo
//              ^? OK
	static { this.foo = {}; }
})

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground から始め、ジェネリック関数とクラス式の例を使って、バージョン 4.0.5 と 4.1.5 の間のリグレッションを比較してください。非プリミティブな静的プロパティがなぜ any と推論されるのかを調査し、ドキュメント化された workaround の動作を維持したまま、例がエラー 7022 なしで A.foo を {} と報告すれば Issue は完了としてください。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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