I am trying to declare global prototype function in Next.js, React, Typescript, but I am getting error <object>.<functionname> is not a function.
I have a file ArrayFunc.ts, in which I have:
export {}
declare global {
interface Array<T> {
distinct(): T[]
}
}
Array.prototype.distinct = function (): Array<any> {
return Array.from(new Set<T>(array))
}
export function importArrayFunctions () {}
then I have Page:
'use client'
export const MyPage: FC<{data: number[]}> = ({ data }) => {
return <div>{data.distinct().length}</div> -- error here
}
If I have only this, it does not work.
I have to call importArrayFunctions() at beginning to make it work in every Page file but there is a lot of files and lot of prototype functions.
Is it possible to do it better way? Or how to declare prototype function correctly so server side and client side can see it?
importArrayFunctions()at begining to make it work" - no, but you have to at leastimportthe module that does the prototype modifications; otherwise that code won't run. You haven't shown that part of your page file.