angularecmascript-6eslintecmascript-2021

details.features.has is not a function


I am using angular 13 and es2021(also tried es6 to add 2018)

tsconfig.lib.json

...
"lib": ["dom", "es2021"]
...

I have following object.

export interface UserDetails {
  ...
  features?: Set<UserFeature>;
  ...
}

I am trying to check some values as below:

this.sessionQuery.details$.subscribe((details: UserDetails) => {
      const enableFF = details.features && (details.features as Set<UserFeature>).has({ feature: 'enableFF ', enabled: true });
 ...
    });

there is no issue on IDE but i have following issue at browser:

core.mjs:6495 ERROR TypeError: details.features.has is not a function


Solution

  • There is a possibility that the object you receive in your subscription (next) doesn't pass the features as a Set. You then afterwards tell typescript to consider your features as a Set, and doing so, you open up your code for such runtime exceptions.

    If there is no way to receive an actual Set object in your subscription, you will have to instantiate a set containing your data or adjust your logic to not depend on a Set method.