There seems to be an issue with a method that is being called from two different places but returns false for both argument types. Despite checking for the correct types, the problem persists and I am unsure why. Although I have read a similar question on StackOverflow, my situation differs as I am not constructing a plain object like the other user mentioned. You can find the related question here:
TypeScript instanceof not working
Below is the problematic method:
changeUnitOfValueInput(filter){
console.log((filter === undefined));
let filterType: string;
if (filter instanceof HTMLInputElement)
filterType = (filter as HTMLInputElement).value;
else if (filter instanceof AdvancedFilter){
console.log("its in");
filterType = (filter as AdvancedFilter).advancedFilterSubject;
}
else{
console.log('im out');
return;
}
switch(filterType){
case AdvancedFilterSubject.GROWTH_TEMPERATURE:{
this.unitLabel = (this.filterOptionsForm.controls['measureSystem'].value ==='imperial') ? '°F' : '°C';
break;
}
case AdvancedFilterSubject.GROWTH_DAYS:{
this.unitLabel = 'days'
break;
}
//fall through
case AdvancedFilterSubject.PLANT_HEIGHT:
case AdvancedFilterSubject.PLANT_SPACING:
case AdvancedFilterSubject.ROW_SPACING:
case AdvancedFilterSubject.SOW_DEPTH:
this.unitLabel = (this.filterOptionsForm.controls['measureSystem'].value ==='imperial') ? '″' : '㎝';
}
}
This method is invoked within the following component method:
populateFieldsWithSelectedFilterData(existingAdvancedFilter: AdvancedFilter){
this.changeUnitOfValueInput(existingAdvancedFilter);
this.filterOptionsForm.controls['filterType'].setValue(existingAdvancedFilter.advancedFilterSubject);
this.filterOptionsForm.controls['logicalOperator'].setValue(existingAdvancedFilter.logicalOperator);
this.filterOptionsForm.controls['advancedFilterValue'].setValue(existingAdvancedFilter.filterValue);
}
The second invocation takes place in the frontend (HTML part):
<p-dropdown #filterType formControlName = "filterType" placeholder="Filter type" [options]="filterTypesList" (onChange)="changeUnitOfValueInput(filterType)" >
</p-dropdown>
Despite the checks using instanceof
, the method continues to return prematurely as both conditions evaluate to false. Can anyone shed light on what might be causing this behavior?
EDIT: Here is the definition of filterTypeList
utilized in the HTML:
filterTypesList: SelectItem[] = [
{label:'Minimum temperature', value: AdvancedFilterSubject.GROWTH_TEMPERATURE},
{label:'Growth days', value: AdvancedFilterSubject.GROWTH_DAYS},
{label:'Maximum height', value: AdvancedFilterSubject.PLANT_HEIGHT},
{label:'Row spacing', value: AdvancedFilterSubject.ROW_SPACING},
{label:'Plant spacing', value: AdvancedFilterSubject.PLANT_SPACING},
{label:'Sow depth', value: AdvancedFilterSubject.SOW_DEPTH}
];