MCPcopy Create free account
hub / github.com/angular/angular / generateTypeCtorDeclaration

Function generateTypeCtorDeclaration

packages/compiler/test/typecheck/util.ts:339–396  ·  view source on GitHub ↗

Generates a declared (non-inline) type constructor for a directive.

(
  env: TcbEnvironment,
  meta: TypeCtorMetadata,
  nodeTypeRef: TcbExpr,
  typeParams: TcbTypeParameter[] | undefined,
)

Source from the content-addressed store, hash-verified

337
338/** Generates a declared (non-inline) type constructor for a directive. */
339function generateTypeCtorDeclaration(
340 env: TcbEnvironment,
341 meta: TypeCtorMetadata,
342 nodeTypeRef: TcbExpr,
343 typeParams: TcbTypeParameter[] | undefined,
344): TcbExpr {
345 const typeRef = nodeTypeRef.print();
346 const typeRefWithGenerics =
347 typeParams === undefined || typeParams.length === 0
348 ? typeRef
349 : `${typeRef}<${typeParams.map((param) => param.name).join(', ')}>`;
350 const typeParamsWithDefaults =
351 typeParams === undefined || typeParams.length === 0
352 ? ''
353 : `<${typeParams.map((param) => param.representationWithDefault).join(', ')}>`;
354
355 const plainKeys: string[] = [];
356 const coercedKeys: string[] = [];
357 const signalInputKeys: string[] = [];
358
359 for (const {classPropertyName, transformType, isSignal} of meta.fields.inputs) {
360 if (isSignal) {
361 signalInputKeys.push(TcbExpr.quoteAndEscape(classPropertyName));
362 } else if (!meta.coercedInputFields.has(classPropertyName)) {
363 plainKeys.push(TcbExpr.quoteAndEscape(classPropertyName));
364 } else {
365 const propName = `ngAcceptInputType_${classPropertyName}`;
366 const isUnsafe = isUnsafeObjectKey(classPropertyName);
367 const access = isUnsafe ? `[${TcbExpr.quoteAndEscape(propName)}]` : `.${propName}`;
368 const coercionType = transformType ?? `typeof ${typeRef}${access}`;
369 const key = isUnsafe ? TcbExpr.quoteAndEscape(classPropertyName) : classPropertyName;
370 coercedKeys.push(`${key}: ${coercionType}`);
371 }
372 }
373
374 let initType: string | null = null;
375 if (plainKeys.length > 0) {
376 initType = `Pick<${typeRefWithGenerics}, ${plainKeys.join(' | ')}>`;
377 }
378 if (coercedKeys.length > 0) {
379 const coercedLiteral = `{\n${coercedKeys.map((key) => `${key};\n`).join('')}}`;
380 initType = initType !== null ? `${initType} & ${coercedLiteral}` : coercedLiteral;
381 }
382 if (signalInputKeys.length > 0) {
383 const unwrapRef = env.referenceExternalSymbol(
384 R3Identifiers.UnwrapDirectiveSignalInputs.moduleName,
385 R3Identifiers.UnwrapDirectiveSignalInputs.name,
386 );
387 const unwrapExpr = `${unwrapRef.print()}<${typeRefWithGenerics}, ${signalInputKeys.join(' | ')}>`;
388 initType = initType !== null ? `${initType} & ${unwrapExpr}` : unwrapExpr;
389 }
390
391 const initParam = `init: ${initType ?? '{}'}`;
392 const source = meta.body
393 ? `const ${meta.fnName}: ${typeParamsWithDefaults}(${initParam}) => ${typeRefWithGenerics} = null!`
394 : `declare function ${meta.fnName}${typeParamsWithDefaults}(${initParam}): ${typeRefWithGenerics}`;
395 return new TcbExpr(source);
396}

Callers 1

typeCtorForMethod · 0.85

Calls 8

isUnsafeObjectKeyFunction · 0.90
mapMethod · 0.80
quoteAndEscapeMethod · 0.80
joinMethod · 0.65
hasMethod · 0.65
printMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected