refactor(ivy): consistently translate types to ts.TypeNode
(#34021)
The compiler has a translation mechanism to convert from an Angular `Type` to a `ts.TypeNode`, as appropriate. Prior to this change, it would translate certain Angular expressions into their value equivalent in TypeScript, instead of the correct type equivalent. This was possible as the `ExpressionVisitor` interface is not strictly typed, with `any`s being used for return values. For example, a literal object was translated into a `ts.ObjectLiteralExpression`, containing `ts.PropertyAssignment` nodes as its entries. This has worked without issues as their printed representation is identical, however it was incorrect from a semantic point of view. Instead, a `ts.TypeLiteralNode` is created with `ts.PropertySignature` as its members, which corresponds with the type declaration of an object literal. PR Close #34021
This commit is contained in:
@ -205,6 +205,7 @@ export class IvyDeclarationDtsTransform implements DtsTransform {
|
||||
const newMembers = fields.map(decl => {
|
||||
const modifiers = [ts.createModifier(ts.SyntaxKind.StaticKeyword)];
|
||||
const typeRef = translateType(decl.type, imports);
|
||||
emitAsSingleLine(typeRef);
|
||||
return ts.createProperty(
|
||||
/* decorators */ undefined,
|
||||
/* modifiers */ modifiers,
|
||||
@ -225,6 +226,11 @@ export class IvyDeclarationDtsTransform implements DtsTransform {
|
||||
}
|
||||
}
|
||||
|
||||
function emitAsSingleLine(node: ts.Node) {
|
||||
ts.setEmitFlags(node, ts.EmitFlags.SingleLine);
|
||||
ts.forEachChild(node, emitAsSingleLine);
|
||||
}
|
||||
|
||||
export class ReturnTypeTransform implements DtsTransform {
|
||||
private typeReplacements = new Map<ts.Declaration, Type>();
|
||||
|
||||
|
Reference in New Issue
Block a user