microsoft / microsoft/TypeScript

Methods in simple mixin result as type any.

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

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

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 3.9.2

Search Terms:

typescript mixin method is any

Code

type AnyCtor = new (...a: any[]) => any

function Foo<T extends AnyCtor>(Base: T) {
	return class Foo extends Base {
		foo() {}
	}
}
function Bar<T extends AnyCtor>(Base: T) {
	return class Bar extends Base {
		bar() {}
	}
}
function One<T extends AnyCtor>(Base: T) {
	return class One extends Base {
		one() {}
	}
}
function Two<T extends AnyCtor>(Base: T) {
	return class Two extends Base {
		two() {}
	}
}
function Three<T extends AnyCtor>(Base: T) {
	return class Three extends One(Two(Base)) {
		three() {}
	}
}

class MyClass extends Three(Foo(Bar(Object))) {
	test() {
		// @ts-expect-error
		this.foo(123)
		// @ts-expect-error
		this.bar(123)
		// @ts-expect-error // ERROR
		this.one(123) // this.one is type `any`!
		// @ts-expect-error // ERROR
		this.two(123) // this.two is type `any`!
		// @ts-expect-error
		this.three(123)
		console.log('no runtime errors')
	}
}

const m = new MyClass()
m.test()

Expected behavior:

There should be an error on all the lines marked with // @ts-expect-error

Actual behavior:

There is no type error on the lines with this.one and this.two because this.one and this.two are seen as type any.

The expectation is that the one and two methods have the proper type (with zero parameters) which would therefore cause a type error from passing in arguments.

Playground Link

Related Issues:

Someone from the Discord chat thought perhaps https://github.com/microsoft/TypeScript/pull/29571 might be related, but not with 100% certainty. They do however think this is a bug.

Ultimately, making mixins in TypeScript is too hard. It's too easy to get them wrong and they become a very inconvenient in TypeScript (whereas they are very convenient in plain JavaScript).

Also related: https://github.com/microsoft/TypeScript/issues/32080

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

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

はじめの一歩

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

調査の方向性

リンクされている Playground の再現から始め、issue 32080 と pull request 29571 で説明されている動作を比較します。ネストされた mixin 呼び出しが onetwo の型をどのように推論するかを追跡します。6 行すべての @ts-expect-error 行が型エラーを生成し、追加の引数を指定した呼び出しも含まれる状態が完了条件です。

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

評価

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

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

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