fix(ivy): use 'typeof' and 'never' for type metadata (#24862)

Previously ngtsc would use a tuple of class types for listing metadata
in .d.ts files. For example, an @NgModule's declarations might be
represented with the type:

[NgIf, NgForOf, NgClass]

If the module had no declarations, an empty tuple [] would be produced.

This has two problems.

1. If the class type has generic type parameters, TypeScript will
complain that they're not provided.

2. The empty tuple type is not actually legal.

This commit addresses both problems.

1. Class types are now represented using the `typeof` operator, so the
above declarations would be represented as:

[typeof NgIf, typeof NgForOf, typeof NgClass].

Since typeof operates on a value, it doesn't require generic type
arguments.

2. Instead of an empty tuple, `never` is used to indicate no metadata.

PR Close #24862
This commit is contained in:
Alex Rickabaugh
2018-07-17 13:34:20 -07:00
committed by Victor Berchet
parent d3594fc1c5
commit ed1db40322
13 changed files with 85 additions and 14 deletions

View File

@ -74,8 +74,8 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
})]);
const type = new o.ExpressionType(o.importExpr(R3.NgModuleDef, [
new o.ExpressionType(moduleType), new o.ExpressionType(o.literalArr(declarations)),
new o.ExpressionType(o.literalArr(imports)), new o.ExpressionType(o.literalArr(exports))
new o.ExpressionType(moduleType), tupleTypeOf(declarations), tupleTypeOf(imports),
tupleTypeOf(exports)
]));
const additionalStatements: o.Statement[] = [];
@ -147,3 +147,8 @@ function accessExportScope(module: o.Expression): o.Expression {
const selectorScope = new o.ReadPropExpr(module, 'ngModuleDef');
return new o.ReadPropExpr(selectorScope, 'exported');
}
function tupleTypeOf(exp: o.Expression[]): o.Type {
const types = exp.map(type => o.typeofExpr(type));
return exp.length > 0 ? o.expressionType(o.literalArr(types)) : o.NONE_TYPE;
}