export function isStructuralSuperType()

in packages/jsii-diff/lib/type-analysis.ts [167:206]


export function isStructuralSuperType(
  a: reflect.InterfaceType,
  b: reflect.InterfaceType,
  updatedSystem: reflect.TypeSystem,
): Analysis {
  // We know all members can only be properties, so that makes it easier.
  const bProps = b.getProperties(true);

  // Use timing words in error message to make it more understandable
  const formerly = b.system === updatedSystem ? 'formerly' : 'newly';
  const is = b.system === updatedSystem ? 'is' : 'used to be';
  const removed = b.system === updatedSystem ? 'removed' : 'added';

  for (const [name, aProp] of Object.entries(a.getProperties(true))) {
    const bProp = bProps[name];

    if (aProp.optional) {
      // Optional field, only requirement is that IF it exists, the type must match.
      if (!bProp) {
        continue;
      }
    } else {
      if (!bProp) {
        return failure(`${formerly} required property '${name}' ${removed}`);
      }
      if (bProp.optional) {
        return failure(
          `${formerly} required property '${name}' ${is} optional`,
        );
      }
    }

    const ana = isSuperType(aProp.type, bProp.type, updatedSystem);
    if (!ana.success) {
      return failure(`property ${name}`, ...ana.reasons);
    }
  }

  return { success: true };
}