声明Typescript库的扩展



我正在使用Sequence的extendSequence()实用程序向所有Sequence实例添加一个自定义方法:

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)

它在开发模式(Next.js开发模式(下工作,但我的IDE(WebStorm(和构建过程都失败了,并出现了一个错误,称自定义方法不存在:

asSequence([1,2,3]).equals([1,2,3])
^^^^^^
TS2339: Property 'equals' does not exist on type 'Sequence '.

我尝试将一个定义与原始接口合并,并将其与前一段代码一起导入,这段代码实际上实现了它,但我缺少了一些东西,因为IDE和构建工具都忽略了它:

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
}
}

将自定义方法合并到导入接口中的正确方法是什么?

我发现了问题。

由于序列接口定义如下:

export default interface Sequence<T> ...

我还必须在我的扩展中包含完全相同的出口限定符:

declare module 'sequency' {
export default interface Sequence<T> {
// ^^^^^^^^^^^
...
}
}

最新更新