I have a class that I'm using in multiple places in my application.
import { TemplateRef } from '@angular/core';
export class ObjectPropertyDisplayStrategy<T> {
content: TemplateRef<any> | ((record: T) => string) | string;
displayStrategyType: DisplayStrategyType;
constructor(content: TemplateRef<any> | ((record: T) => string) | string) {
this.content = content;
setDisplayStrategyType(this, content);
}
}
As can be seen by the Typescript annotations, the constructor expects a parameter to be passed to it that is of type TemplateRef
or Function
or String
. It then passes this parameter to a function setDisplayStrategyType
function setDisplayStrategyType(
displayStrategy: { displayStrategyType: DisplayStrategyType },
content: TemplateRef<any> | ((record: any) => string) | string
) {
if (Object.getPrototypeOf(content).constructor.name === 'TemplateRef_') {
displayStrategy.displayStrategyType = DisplayStrategyType.Template;
return;
}
switch (typeof content) {
case typeof Function:
displayStrategy.displayStrategyType = DisplayStrategyType.FunctionTransform;
break;
case typeof String:
default:
displayStrategy.displayStrategyType = DisplayStrategyType.String;
break;
}
}
This function tries to determine which of the three types was passed as the parameter and then sets an enum value which allows some display logic to occur in an Angular component.
For the TemplateRef
type specifically, it is being determined through the following logic: if (Object.getPrototypeOf(content).constructor.name === 'TemplateRef_')
. This works when I build the project using ng build
. When I run ng build --prod
though, it stops working and seems to be due to the fact that when everything gets uglified, the TemplateRef_
object gets changed to something like e
. This issue is mentioned in the first comment on the answer here.
I've tried utilizing the TemplateRef
symbol to deal with this issue as follows:
const a = typeof TemplateRef;
const b = new TemplateRef();
const c = Object.getPrototypeOf(TemplateRef);
As well as the ViewChild
decorator as that seems to be one way in Angular to create a TemplateRef
instance.
const a = typeof ViewChild;
const b = new ViewChild();
const c = Object.getPrototypeOf(ViewChild);
None of these strategies seem to work though. Is there a reliable way to construct a new TemplateRef
object that I could compare 'types' with my passed property or another way to make this determination so that my library can continue to work with a prod build?