/
type_constructor.ts
282 lines (255 loc) Β· 11 KB
/
type_constructor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ExpressionType, R3Identifiers, WrappedNodeExpr} from '@angular/compiler';
import ts from 'typescript';
import {ClassDeclaration, ReflectionHost} from '../../reflection';
import {TypeCtorMetadata} from '../api';
import {ReferenceEmitEnvironment} from './reference_emit_environment';
import {checkIfGenericTypeBoundsCanBeEmitted} from './tcb_util';
import {tsCreateTypeQueryForCoercedInput} from './ts_util';
export function generateTypeCtorDeclarationFn(
env: ReferenceEmitEnvironment, meta: TypeCtorMetadata, nodeTypeRef: ts.EntityName,
typeParams: ts.TypeParameterDeclaration[]|undefined): ts.Statement {
const rawTypeArgs = typeParams !== undefined ? generateGenericArgs(typeParams) : undefined;
const rawType = ts.factory.createTypeReferenceNode(nodeTypeRef, rawTypeArgs);
const initParam = constructTypeCtorParameter(env, meta, rawType);
const typeParameters = typeParametersWithDefaultTypes(typeParams);
if (meta.body) {
const fnType = ts.factory.createFunctionTypeNode(
/* typeParameters */ typeParameters,
/* parameters */[initParam],
/* type */ rawType,
);
const decl = ts.factory.createVariableDeclaration(
/* name */ meta.fnName,
/* exclamationToken */ undefined,
/* type */ fnType,
/* body */ ts.factory.createNonNullExpression(ts.factory.createNull()));
const declList = ts.factory.createVariableDeclarationList([decl], ts.NodeFlags.Const);
return ts.factory.createVariableStatement(
/* modifiers */ undefined,
/* declarationList */ declList);
} else {
return ts.factory.createFunctionDeclaration(
/* modifiers */[ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
/* asteriskToken */ undefined,
/* name */ meta.fnName,
/* typeParameters */ typeParameters,
/* parameters */[initParam],
/* type */ rawType,
/* body */ undefined);
}
}
/**
* Generate an inline type constructor for the given class and metadata.
*
* An inline type constructor is a specially shaped TypeScript static method, intended to be placed
* within a directive class itself, that permits type inference of any generic type parameters of
* the class from the types of expressions bound to inputs or outputs, and the types of elements
* that match queries performed by the directive. It also catches any errors in the types of these
* expressions. This method is never called at runtime, but is used in type-check blocks to
* construct directive types.
*
* An inline type constructor for NgFor looks like:
*
* static ngTypeCtor<T>(init: Pick<NgForOf<T>, 'ngForOf'|'ngForTrackBy'|'ngForTemplate'>):
* NgForOf<T>;
*
* A typical constructor would be:
*
* NgForOf.ngTypeCtor(init: {
* ngForOf: ['foo', 'bar'],
* ngForTrackBy: null as any,
* ngForTemplate: null as any,
* }); // Infers a type of NgForOf<string>.
*
* Any inputs declared on the type for which no property binding is present are assigned a value of
* type `any`, to avoid producing any type errors for unset inputs.
*
* Inline type constructors are used when the type being created has bounded generic types which
* make writing a declared type constructor (via `generateTypeCtorDeclarationFn`) difficult or
* impossible.
*
* @param node the `ClassDeclaration<ts.ClassDeclaration>` for which a type constructor will be
* generated.
* @param meta additional metadata required to generate the type constructor.
* @returns a `ts.MethodDeclaration` for the type constructor.
*/
export function generateInlineTypeCtor(
env: ReferenceEmitEnvironment, node: ClassDeclaration<ts.ClassDeclaration>,
meta: TypeCtorMetadata): ts.MethodDeclaration {
// Build rawType, a `ts.TypeNode` of the class with its generic parameters passed through from
// the definition without any type bounds. For example, if the class is
// `FooDirective<T extends Bar>`, its rawType would be `FooDirective<T>`.
const rawTypeArgs =
node.typeParameters !== undefined ? generateGenericArgs(node.typeParameters) : undefined;
const rawType = ts.factory.createTypeReferenceNode(node.name, rawTypeArgs);
const initParam = constructTypeCtorParameter(env, meta, rawType);
// If this constructor is being generated into a .ts file, then it needs a fake body. The body
// is set to a return of `null!`. If the type constructor is being generated into a .d.ts file,
// it needs no body.
let body: ts.Block|undefined = undefined;
if (meta.body) {
body = ts.factory.createBlock([
ts.factory.createReturnStatement(ts.factory.createNonNullExpression(ts.factory.createNull())),
]);
}
// Create the type constructor method declaration.
return ts.factory.createMethodDeclaration(
/* modifiers */[ts.factory.createModifier(ts.SyntaxKind.StaticKeyword)],
/* asteriskToken */ undefined,
/* name */ meta.fnName,
/* questionToken */ undefined,
/* typeParameters */ typeParametersWithDefaultTypes(node.typeParameters),
/* parameters */[initParam],
/* type */ rawType,
/* body */ body,
);
}
function constructTypeCtorParameter(
env: ReferenceEmitEnvironment, meta: TypeCtorMetadata,
rawType: ts.TypeReferenceNode): ts.ParameterDeclaration {
// initType is the type of 'init', the single argument to the type constructor method.
// If the Directive has any inputs, its initType will be:
//
// Pick<rawType, 'inputA'|'inputB'>
//
// Pick here is used to select only those fields from which the generic type parameters of the
// directive will be inferred.
//
// In the special case there are no inputs, initType is set to {}.
let initType: ts.TypeNode|null = null;
const plainKeys: ts.LiteralTypeNode[] = [];
const coercedKeys: ts.PropertySignature[] = [];
const signalInputKeys: ts.LiteralTypeNode[] = [];
for (const {classPropertyName, transform, isSignal} of meta.fields.inputs) {
if (isSignal) {
signalInputKeys.push(
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(classPropertyName)));
} else if (!meta.coercedInputFields.has(classPropertyName)) {
plainKeys.push(
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(classPropertyName)));
} else {
const coercionType = transform != null ?
transform.type.node :
tsCreateTypeQueryForCoercedInput(rawType.typeName, classPropertyName);
coercedKeys.push(ts.factory.createPropertySignature(
/* modifiers */ undefined,
/* name */ classPropertyName,
/* questionToken */ undefined,
/* type */ coercionType));
}
}
if (plainKeys.length > 0) {
// Construct a union of all the field names.
const keyTypeUnion = ts.factory.createUnionTypeNode(plainKeys);
// Construct the Pick<rawType, keyTypeUnion>.
initType = ts.factory.createTypeReferenceNode('Pick', [rawType, keyTypeUnion]);
}
if (coercedKeys.length > 0) {
const coercedLiteral = ts.factory.createTypeLiteralNode(coercedKeys);
initType = initType !== null ?
ts.factory.createIntersectionTypeNode([initType, coercedLiteral]) :
coercedLiteral;
}
if (signalInputKeys.length > 0) {
const keyTypeUnion = ts.factory.createUnionTypeNode(signalInputKeys);
// Construct the UnwrapDirectiveSignalInputs<rawType, keyTypeUnion>.
const unwrapDirectiveSignalInputsExpr = env.referenceExternalType(
R3Identifiers.UnwrapDirectiveSignalInputs.moduleName,
R3Identifiers.UnwrapDirectiveSignalInputs.name, [
// TODO:
new ExpressionType(new WrappedNodeExpr(rawType)),
new ExpressionType(new WrappedNodeExpr(keyTypeUnion))
]);
initType = initType !== null ?
ts.factory.createIntersectionTypeNode([initType, unwrapDirectiveSignalInputsExpr]) :
unwrapDirectiveSignalInputsExpr;
}
if (initType === null) {
// Special case - no inputs, outputs, or other fields which could influence the result type.
initType = ts.factory.createTypeLiteralNode([]);
}
// Create the 'init' parameter itself.
return ts.factory.createParameterDeclaration(
/* modifiers */ undefined,
/* dotDotDotToken */ undefined,
/* name */ 'init',
/* questionToken */ undefined,
/* type */ initType,
/* initializer */ undefined);
}
function generateGenericArgs(params: ReadonlyArray<ts.TypeParameterDeclaration>): ts.TypeNode[] {
return params.map(param => ts.factory.createTypeReferenceNode(param.name, undefined));
}
export function requiresInlineTypeCtor(
node: ClassDeclaration<ts.ClassDeclaration>, host: ReflectionHost,
env: ReferenceEmitEnvironment): boolean {
// The class requires an inline type constructor if it has generic type bounds that can not be
// emitted into the provided type-check environment.
return !checkIfGenericTypeBoundsCanBeEmitted(node, host, env);
}
/**
* Add a default `= any` to type parameters that don't have a default value already.
*
* TypeScript uses the default type of a type parameter whenever inference of that parameter
* fails. This can happen when inferring a complex type from 'any'. For example, if `NgFor`'s
* inference is done with the TCB code:
*
* ```
* class NgFor<T> {
* ngForOf: T[];
* }
*
* declare function ctor<T>(o: Pick<NgFor<T>, 'ngForOf'|'ngForTrackBy'|'ngForTemplate'>):
* NgFor<T>;
* ```
*
* An invocation looks like:
*
* ```
* var _t1 = ctor({ngForOf: [1, 2], ngForTrackBy: null as any, ngForTemplate: null as any});
* ```
*
* This correctly infers the type `NgFor<number>` for `_t1`, since `T` is inferred from the
* assignment of type `number[]` to `ngForOf`'s type `T[]`. However, if `any` is passed instead:
*
* ```
* var _t2 = ctor({ngForOf: [1, 2] as any, ngForTrackBy: null as any, ngForTemplate: null as
* any});
* ```
*
* then inference for `T` fails (it cannot be inferred from `T[] = any`). In this case, `T`
* takes the type `{}`, and so `_t2` is inferred as `NgFor<{}>`. This is obviously wrong.
*
* Adding a default type to the generic declaration in the constructor solves this problem, as
* the default type will be used in the event that inference fails.
*
* ```
* declare function ctor<T = any>(o: Pick<NgFor<T>, 'ngForOf'>): NgFor<T>;
*
* var _t3 = ctor({ngForOf: [1, 2] as any});
* ```
*
* This correctly infers `T` as `any`, and therefore `_t3` as `NgFor<any>`.
*/
function typeParametersWithDefaultTypes(params: ReadonlyArray<ts.TypeParameterDeclaration>|
undefined): ts.TypeParameterDeclaration[]|undefined {
if (params === undefined) {
return undefined;
}
return params.map(param => {
if (param.default === undefined) {
return ts.factory.updateTypeParameterDeclaration(
param, param.modifiers, param.name, param.constraint,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
} else {
return param;
}
});
}