Home > other >  Vue/TS filter function
Vue/TS filter function

Time:10-19

I have some struggles with assigning a type to filter function with TypeScript. So, when I'm building the app, it says like:

Parameter 'item' implicitly has an 'any' type.

And function looks like:

allowedProductPaymentMethods?.filter(item => item.id !== paymentItem.id && item.balance > 0)

So I tried adding like (item: any), but it's not recognizing the typing. Any ideas how to fix it?

CodePudding user response:

if allowedProductPaymentMethods is computed property you should add .value :

allowedProductPaymentMethods.value?.filter(item => item.id !== paymentItem.id && item.balance > 0)

CodePudding user response:

If allowedProductPaymentMethods is typed, then, item type is inferred. TS inference

If not, then you can type item inside your filter (item: TypeYouWant)

Hope it helps.

CodePudding user response:

I believe this is a linting issue and not a typescript issue.
You are able to override the compiler options in the tsconfig.json which should prevent this from being an issue during compilation.

The solution to your issue should be simply adding the type to your iteration variable:

const allowedProductPaymentMethods: ComputedRef<Array<IPaymentListItem>>
allowedProductPaymentMethods?.filter((item: IPaymentListItem) => item.id !== paymentItem.id && item.balance > 0)

It does appear that you are also missing the value in your function when invoking the Computed object Vue docs:

const allowedProductPaymentMethods: ComputedRef<Array<IPaymentListItem>>
allowedProductPaymentMethods?.value?.filter((item: IPaymentListItem) => item.id !== paymentItem.id && item.balance > 0)
  • Related