-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Describe the bug
Affected Component: FilterService (PrimeNG DataTable / DataView filtering)
File/Context:
filterservice.ts → inside filter functions like startsWith, endsWith, etc.
Problem
Some filter functions (e.g., startsWith, endsWith, contains) assume that the filter argument is always a string and directly call .trim() on it.
However, when the filter value is of a non-string type (e.g., number, boolean, object), this leads to runtime errors such as:
ERROR TypeError: filter.trim is not a function
This occurs when filtering numeric or non-string columns using these filter rules.
Root Cause
Code before:
if (filter === undefined || filter === null || filter.trim() === '') {
return true;
}
No type safety check — .trim() is called even if filter is not a string.
Fix
Added a type guard to ensure .trim() is only called when filter is a string:
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
fix(filterservice): add string type check in filter conditions to prevent .trim() errors when filter is not a string.
Pull Request Link
Reason for not contributing a PR
- Lack of time
- Unsure how to implement the fix/feature
- Difficulty understanding the codebase
- Other
Other Reason
No response
Reproducer
https://stackblitz.com/edit/github-ltjgwhjb?file=src%2Fapp%2Fapp.component.html
Environment
Testing
Angular version
19.2.15
PrimeNG version
v19 (LTS Only)
Node version
19.2.15
Browser(s)
No response
Steps to reproduce the behavior
Switch from async data for the data of the table to offline mode
Expected behavior
should work as before