microsoft / microsoft/TypeScript

Allow base class expressions to reference class type parameters as long as the type param is not used on the static side of the resulting class

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

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

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

説明

🔎 Search Terms

"Base class expressions cannot reference class type parameters" when stacking dynamic classes (mixins) with type parameters
Allow base class expressions to reference class type parameters as long as the type param is not used on the static side of the resulting class

Most closely related issues, but are IMO a different situation (as I explain below):
https://github.com/microsoft/TypeScript/issues/26154
https://github.com/microsoft/TypeScript/issues/17829 (documented at https://www.typescriptlang.org/docs/handbook/mixins.html#static-property-mixins-17829)

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed all the FAQ entries
⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oCrgQA8eAfFALxQCGARiqtdlAhAO5QAUAdL9agHMkALhoIQAbQC6ASkrk8AbgBQyzIhRQAQvzoAbCADFKUEjqTQIAD2AQEAEySwNaLDnyEi1caVKdzEKJ4AXIU5ADeylDRNPRoTMBQmHrUSE46jLQGRAAa5Na2Dump0JEx5VC0-JxWojly4VCoEMDoqAhQVopQAL5RMX3lza3t2rpZECo9KmouUIZwcPpGJmYlUAV2js7Irti4BJBePn4BQSHyUGUxdAwJSSlp84vLufk2W8UWV-0VUABmixqdQaTRabQ6XV6v2hQ3BowWSwmUxmyVSTjwqQA1m8Nh8imNMgZDJxEcsSQB5WgAKwg2BkbwZeSuPSgAHo2VAwKg4BMALZQACWTkQUGAAAthVA9ILWKogA

💻 Code
type ConstructorType<T> = abstract new (...args: any[]) => T;

const BarableF = <TBase extends ConstructorType<any>>(Base: TBase) => {
    abstract class Barable<X> extends Base {
        bar(x: X) { return x; }
    }
    return Barable;
};

const FooableF = <TBase extends ConstructorType<any>>(Base: TBase) => {
    abstract class Fooable<X> extends Base {
        foo(x: X) { return x; }
    }
    return Fooable;
};

abstract class Task<X> extends BarableF(FooableF(Object)<X>)<X> {} // problem is on this line
🙁 Actual behavior

On line class Task<X> extends BarableF(FooableF(Object)<X>)<X> {} // problem is on this line, the inner-most X raises "Base class expressions cannot reference class type parameters", despite the fact that X is only ever used by the base class expression on the instance side of it's return value (as opposed to the static side). This example expands to:

abstract class Fooable<X> extends Object {
    foo(x: X) { return x; }
}

abstract class Barable<X> extends Fooable<X> {
    bar(x: X) { return x; }
}

abstract class Task<X> extends Barable<X> {}

which is perfectly valid code.

🙂 Expected behavior

I would expect for this to be valid code.

Unlike the situations in https://github.com/microsoft/TypeScript/issues/26154 and https://github.com/microsoft/TypeScript/issues/17829 and mentioned in https://www.typescriptlang.org/docs/handbook/mixins.html#static-property-mixins-17829, the type param here is not being used in the static side of resulting class we are extending.

IMO https://github.com/microsoft/TypeScript/pull/17922 was too restrictive of a fix, and since TS seems to error on the side of unsound-ness, I think there's an argument for this change to be reverted until there's a proper fix.

The full correct fix would be to allow type params in the base class expression as long as the result of the base class expression does not reference the type param anywhere on the static side of the class.

A middle-ground fix that would solve my use case in particular would be to allow type params in the base class expression if the expression they are used with is a class-like type (as opposed to a regular function that returns a class). Because TS already checks that class type params aren't used in static members (error 2302), this should be fine. However, that wouldn't solve this case:

function base<T>() {
    class Base {
        foo(t: T): T { return t;}
    }
    return Base;
}

class Gen<T> extends base<T>() {}

where again, the type param is only used on the instance side of the resulting class

Additional information about the issue

No response

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

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

はじめの一歩

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

調査の方向性

リンクされた Playground と issue 内の最小限の mixin 再現から始め、関連する issue #26154 と #17829、および参照されている PR #17922 を比較してください。Task の例が型チェックを通過し、静的側の使用は引き続き拒否されること(issue で説明されている、関数を返すクラスのケースを含む)が完了条件です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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