typescript#LiteralTypeNode TypeScript Examples

The following examples show how to use typescript#LiteralTypeNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: entity-views-generation.ts    From jmix-frontend with Apache License 2.0 5 votes vote down vote up
/**
 * e.g.
 *
 * Pick<Entity, 'id', 'name'>
 *
 * @param className
 * @param view
 * @param allowedAttrs As of CUBA specific - project model could contains properties from inheritors, we need to
 * filter out such properties from generated views
 * @param idAttributeName
 */
function createPickPropertiesType(
  className: string, view: View, allowedAttrs: EntityAttribute[], idAttributeName: string = 'id'
): ts.TypeReferenceNode {

  const viewProperties = idAttributeName === 'id'
    ? [...view.allProperties]
    : view.allProperties.filter(property => property.name !== idAttributeName);

  if (!viewProperties.find(nameEqId) && allowedAttrs.find(nameEqIdAttrName(idAttributeName))) {
    viewProperties.unshift({name: 'id'});
  }

  const viewTypeNodes: LiteralTypeNode[] = viewProperties
    .filter(viewProperty => allowedAttrs.some(attr => {
      const name = (idAttributeName !== 'id' && viewProperty.name === 'id')
        ? idAttributeName
        : viewProperty.name;
      return attr.name === name;
    }))
    .map(property =>
      ts.createLiteralTypeNode(
        ts.createLiteral(property.name)
      )
    );

  return ts.createTypeReferenceNode(
    'Pick',
    [
      ts.createTypeReferenceNode(className, undefined),
      ts.createUnionTypeNode(viewTypeNodes)
    ]
  );
}