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

Function constructTypeCtorParameter

packages/compiler-cli/preprocessor/src/tcb.ts:416–473  ·  view source on GitHub ↗
(
  env: TcbEnvironment,
  meta: TypeCtorMetadata,
  typeRef: string,
  typeRefWithGenerics: string,
)

Source from the content-addressed store, hash-verified

414}
415
416function constructTypeCtorParameter(
417 env: TcbEnvironment,
418 meta: TypeCtorMetadata,
419 typeRef: string,
420 typeRefWithGenerics: string,
421): string {
422 let initType: string | null = null;
423
424 const plainKeys: string[] = [];
425 const coercedKeys: string[] = [];
426 const signalInputKeys: string[] = [];
427
428 for (const {classPropertyName, transformType, isSignal} of meta.fields.inputs) {
429 if (isSignal) {
430 signalInputKeys.push(TcbExpr.quoteAndEscape(classPropertyName));
431 } else if (!meta.coercedInputFields.has(classPropertyName)) {
432 plainKeys.push(TcbExpr.quoteAndEscape(classPropertyName));
433 } else {
434 const propName = `ngAcceptInputType_${classPropertyName}`;
435 const isUnsafe = isUnsafeObjectKey(classPropertyName);
436 const access = isUnsafe ? `[${TcbExpr.quoteAndEscape(propName)}]` : `.${propName}`;
437 const coercionType =
438 transformType !== undefined ? transformType : `typeof ${typeRef}${access}`;
439
440 coercedKeys.push(
441 `${isUnsafe ? TcbExpr.quoteAndEscape(classPropertyName) : classPropertyName}: ${coercionType}`,
442 );
443 }
444 }
445
446 if (plainKeys.length > 0) {
447 initType = `Pick<${typeRefWithGenerics}, ${plainKeys.join(' | ')}>`;
448 }
449 if (coercedKeys.length > 0) {
450 let coercedLiteral = '{\n';
451 for (const key of coercedKeys) {
452 coercedLiteral += `${key};\n`;
453 }
454 coercedLiteral += '}';
455 initType = initType !== null ? `${initType} & ${coercedLiteral}` : coercedLiteral;
456 }
457 if (signalInputKeys.length > 0) {
458 const keyTypeUnion = signalInputKeys.join(' | ');
459
460 const unwrapRef = env.referenceExternalSymbol(
461 R3Identifiers.UnwrapDirectiveSignalInputs.moduleName,
462 R3Identifiers.UnwrapDirectiveSignalInputs.name,
463 );
464 const unwrapExpr = `${unwrapRef.print()}<${typeRefWithGenerics}, ${keyTypeUnion}>`;
465 initType = initType !== null ? `${initType} & ${unwrapExpr}` : unwrapExpr;
466 }
467
468 if (initType === null) {
469 initType = '{}';
470 }
471
472 return `init: ${initType}`;
473}

Callers 1

Calls 7

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

Tested by

no test coverage detected