Utilizing Sequency's extendSequence()
feature to enhance all Sequence instances with a custom method:
import Sequence, { extendSequence, isSequence } from 'sequency'
import equal from '@wry/equality'
class SequencyExtensions {
equals<T>(this: Sequence<T>, other: Sequence<T> | Iterable<T>): boolean {
const as = this.iterator
const bs = isSequence(other) ? other.iterator : other[Symbol.iterator]()
while (true) {
const a = as.next()
const b = bs.next()
if (a.done && b.done) return true
if (a.done !== b.done || !equal(a.value, b.value)) return false
}
}
}
extendSequence(SequencyExtensions)
Although it functions correctly in development mode (Next.js development mode), I encounter an error in both my IDE (WebStorm) and during the build process, indicating that the custom method is not recognized:
asSequence([1,2,3]).equals([1,2,3])
^^^^^^
TS2339: Property 'equals' does not exist on type 'Sequence '.
I attempted to merge a definition with the original interface by importing it alongside the previous code snippet that implements it, however, both the IDE and build tool are failing to acknowledge it:
declare module 'sequency' {
interface Sequence<T> {
/**
* Returns `true` if this sequence is equal to the other sequence or iterable.
*
* @param {Sequence | Iterable} other
* @returns {boolean}
*/
equals<T>(this: Sequence<T>, other: Sequence<T> | Iterable<T>): boolean
}
}
What is the proper method to merge a custom function into an imported interface?