Skip to content

Enhance filter checks for string type in filterservice #19061

@younesmedjtouh

Description

@younesmedjtouh

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

#19060

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Needs TriageIssue will be reviewed by Core Team and a relevant label will be added as soon as possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions