microsoft / microsoft/TypeScript

Mixin expected argument type resolves to never when constrained to constructor of type whose property is typed via a type parameter

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

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

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

説明

TypeScript Version: 3.8.0-dev.20191105

Search Terms: mixin 3.7 type

Code

import * as ts from "typescript";

type Constructor<T> = new (...args: any[]) => T;

export class Node<NodeType extends ts.Node = ts.Node> {
    compilerNode!: NodeType;
}

// BindingNamedNode
export interface BindingNamedNode {
    getName(): string;
}

export function BindingNamedNode<
    TCompilerNode extends ts.Node & { name: ts.BindingName; },
    TBase extends Constructor<Node<TCompilerNode>>
>(
    Base: TBase
): Constructor<BindingNamedNode> & TBase {
    return {} as any;
}

// InitializerableNode
export interface InitializerableNode {
    removeInitializer(): this;
}

export function InitializerableNode<
    TCompilerNode extends ts.Node & { initializer?: ts.Expression; },
    TBase extends Constructor<Node<TCompilerNode>>
>(
    Base: TBase
): Constructor<InitializerableNode> & TBase {
    return {} as any;
}

// BindingElement
export class BindingElement extends InitializerableNode(BindingNamedNode(Node))<ts.BindingElement> {
}

Other Code

Or my original code... I think it's more correct to do the above, but should either of these error?

import * as ts from "typescript";

type Constructor<T> = new (...args: any[]) => T;

export class Node<NodeType extends ts.Node = ts.Node> {
    compilerNode!: NodeType;
}

// BindingNamedNode
export type BindingNamedNodeExtensionType = Node<ts.Node & { name: ts.BindingName; }>;

export interface BindingNamedNode {
    getName(): string;
}

export function BindingNamedNode<T extends Constructor<BindingNamedNodeExtensionType>>(
    Base: T
): Constructor<BindingNamedNode> & T {
    return {} as any;
}

// InitializerableNode
export type InitializerableNodeExtensionType = Node<ts.Node & { initializer?: ts.Expression; }>;

export interface InitializerableNode {
    removeInitializer(): this;
}

export function InitializerableNode<T extends Constructor<InitializerableNodeExtensionType>>(
    Base: T
): Constructor<InitializerableNode> & T {
    return {} as any;
}

// BindingElement
export class BindingElement extends InitializerableNode(BindingNamedNode(Node))<ts.BindingElement> {
}

Expected behavior: No errors, as in TS < 3.7

TS 3.6.3 Playground

Actual behavior:

TS2345: Argument of type 'typeof Node' is not assignable to parameter of type 'never'.

export class BindingElement extends InitializerableNode(BindingNamedNode(Node))<ts.BindingElement> {
                                                                         ~~~~
}

Playground Link: TS 3.8.0-dev.20191105 Playground

Other Comments

The issue does not occur when the type parameter is inlined (Playground).

Also, I wouldn't be surprised if I was doing something wrong here, but this has worked in the past.

Workaround

Pass the class constructor into a function typed like so and the compile error goes away (Playground):

const createBase = <T extends typeof Node>(ctor: T) => InitializerableNode(BindingNamedNode(ctor));
export class BindingElement extends createBase(Node)<ts.BindingElement> {
}

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

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

はじめの一歩

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

調査の方向性

まず、報告された constructor-mixin の例を TypeScript 3.8.0-dev playground で再現し、TypeScript 3.6.3 playground と比較してください。プロパティ型が型パラメーターを使用している場合に、コンストラクター引数がなぜ never に解決されるのかを調査してください。報告された例がジェネリック制約を維持したままエラーなしでコンパイルできれば完了です。

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

評価

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

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

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