microsoft / microsoft/TypeScript
Mixin expected argument type resolves to never when constrained to constructor of type whose property is typed via a type parameter
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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> {
}
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo reportado de constructor-mixin en el playground de TypeScript 3.8.0-dev y compáralo con el playground de TypeScript 3.6.3. Investiga por qué el argumento del constructor se resuelve como never cuando el tipo de la propiedad utiliza un parámetro de tipo; se considera terminado cuando el ejemplo reportado compila sin errores y conserva las restricciones genéricas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100