microsoft / microsoft/TypeScript
Mixin expected argument type resolves to never when constrained to constructor of type whose property is typed via a type parameter
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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> {
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the reported constructor-mixin example in the TypeScript 3.8.0-dev playground and compare it with the TypeScript 3.6.3 playground. Investigate why the constructor argument resolves to never when the property type uses a type parameter; done means the reported example compiles without errors while preserving the generic constraints.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100