microsoft / microsoft/TypeScript
Mixin expected argument type resolves to never when constrained to constructor of type whose property is typed via a type parameter
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
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> {
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das gemeldete constructor-mixin-Beispiel im TypeScript 3.8.0-dev playground nachzustellen und es mit dem TypeScript 3.6.3 playground zu vergleichen. Untersuche, warum das Konstruktorargument zu never aufgelöst wird, wenn der Eigenschaftstyp einen Typparameter verwendet; als erledigt gilt die Aufgabe, wenn das gemeldete Beispiel ohne Fehler kompiliert und dabei die generischen Einschränkungen beibehalten werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100