fix(ivy): types in .d.ts files should account for generics (#24862)

Ivy definition types have a generic type which specifies the return
type of the factory function. For example:

static ngDirectiveDef<NgForOf, '[ngFor][ngForOf]'>

However, in this case NgForOf itself has a type parameter <T>. Thus,
writing the above is incorrect.

This commit modifies ngtsc to understand the genericness of NgForOf and
to write the following:

static ngDirectiveDef<NgForOf<any>, '[ngFor][ngForOf]'>

PR Close #24862
This commit is contained in:
Alex Rickabaugh
2018-07-13 14:49:01 -07:00
committed by Victor Berchet
parent 2b8b647006
commit 41ef75869c
8 changed files with 53 additions and 12 deletions

View File

@ -47,7 +47,11 @@ export class BuiltinType extends Type {
}
export class ExpressionType extends Type {
constructor(public value: Expression, modifiers: TypeModifier[]|null = null) { super(modifiers); }
constructor(
public value: Expression, modifiers: TypeModifier[]|null = null,
public typeParams: Type[]|null = null) {
super(modifiers);
}
visitType(visitor: TypeVisitor, context: any): any {
return visitor.visitExpressionType(this, context);
}
@ -1240,6 +1244,9 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor
visitBuiltinType(type: BuiltinType, context: any): any { return this.visitType(type, context); }
visitExpressionType(type: ExpressionType, context: any): any {
type.value.visitExpression(this, context);
if (type.typeParams !== null) {
type.typeParams.forEach(param => this.visitType(param, context));
}
return this.visitType(type, context);
}
visitArrayType(type: ArrayType, context: any): any { return this.visitType(type, context); }
@ -1496,8 +1503,9 @@ export function importType(
}
export function expressionType(
expr: Expression, typeModifiers: TypeModifier[] | null = null): ExpressionType {
return new ExpressionType(expr, typeModifiers);
expr: Expression, typeModifiers: TypeModifier[] | null = null,
typeParams: Type[] | null = null): ExpressionType {
return new ExpressionType(expr, typeModifiers, typeParams);
}
export function typeofExpr(expr: Expression) {

View File

@ -361,6 +361,11 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor
visitExpressionType(ast: o.ExpressionType, ctx: EmitterVisitorContext): any {
ast.value.visitExpression(this, ctx);
if (ast.typeParams !== null) {
ctx.print(null, '<');
this.visitAllObjects(type => this.visitType(type, ctx), ast.typeParams, ctx, ',');
ctx.print(null, '>');
}
return null;
}