microsoft / microsoft/TypeScript

Different behavior when declare property or method in a class about covariance

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

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

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

説明

TypeScript Version: 4.1.2

Search Terms: In ts doc - type compatibility, it seems like not mentioned about this.

Code

class M1 {
  c: number = 1;
}

export class M2 extends M1 {
  a: number = 1;
}

type Func<M extends M1> = (m: M) => number;

interface IFunc<M extends M1> {
  func1?: Func<M>,
  func2: Func<M>,
}

class R1<M extends M1 = M1> implements IFunc<M> {
  // when delete this property, it will be no error.
  public func1 = (m: M) => {
    return m.c
  }

  public func2(m: M) {
    return m.c
  };
}

type IRConstructor<M extends M1, R extends R1<M>> = new () => R;

export class R2 extends R1<M2> {
}

type A = IRConstructor<M1, R1>;

let a1: A = R2; // error: Type 'typeof R2' is not assignable to type 'IRConstructor<M1, R1<M1>>'.

console.log(a1);

Expected behavior:

Should declare a property or a method in class has same behavior? I'm not sure...

Actually I want to know how can I make these code without error when I have to use functional property.

Actual behavior:

When you just decalre a method which is compatible with IFunc<M1>, it will be fine. But when you declare a functional property which is compatible with IFunc<M1>, it will be an error.

Playground Link:

Or, you can see playground

Related Issues:

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

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

はじめの一歩

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

調査の方向性

Issue でリンクされている TypeScript Playground の再現例と、Issue で参照されている handbook の「Type Compatibility」セクションから始めてください。関数型プロパティ形式とメソッド形式について、報告された generic class の動作を比較し、その違いが意図的なものかどうかを確認してください。期待される動作が決定され、対応する compiler または documentation のカバレッジが用意されていれば完了です。

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

評価

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

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

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