javascript - 如何在打字稿中声明 getAttribute 函数类型
问题描述
我正在使用带有 xpath 模块的nestjs 打字稿。
但是由于 @types/xpath 不存在 xpath 模块类型,所以我声明了我自己的 xpath 类型,如下所示。
import * as xpath from 'xpath';
export class XmlParserService {
private query(q: string): Array<Node | string> {
return xpath.useNamespaces(this.namespaces)(q, this.parsedXml);
}
我在主目录中声明了 xpath 类型。
// ./src/@types/xpath/index.d.ts
declare module 'xpath' {
type SelectedValue = Node | string;
interface XPathSelect {
(expression: string, node?: Node): Array<SelectedValue>;
(expression: string, node: Node, single: true): SelectedValue | undefined;
}
export const select: XPathSelect;
export function useNamespaces(namespaceMap: {
[name: string]: string;
}): XPathSelect;
}
当我使用这个查询函数时,它返回元素对象。
因此我可以使用 getAttribute 和 ownerDocument。
但是,它给出了打字稿错误。
错误:(104, 19) TS2339: 类型 'string | 上不存在属性 'ownerDocument' 节点'。“字符串”类型上不存在属性“ownerDocument”或“getAttribute”。
const currentNode = this.query(currentNodeQuery)[0];
const idAttribute = currentNode.getAttribute('ID') ? 'ID' : 'Id';
const ownerD = currentNode.ownerDocument
我怎样才能为这种情况申报更多?感谢您阅读。
解决方案
推荐阅读
- postgresql - 在 postgres 中使用“同时创建索引”是否有助于将来插入的行免于锁定?
- python - 返回对应于值的最大出现次数的索引,循环遍历每一行
- python - 程序将数组分成N个连续子数组,使每个子数组的和为奇数
- c# - 使用点分隔符将双精度写入文本文件
- amazon-web-services - 跨账户快照复制的数据传输成本如何计算?
- elisp - 在elisp中隐藏一个全局变量
- python-2.7 - 使用 ChromeDriver Selenium CeleryPython 在没有 setblocking() 方法的文件对象上设置 set_nonblocking()(Windows 管道不支持非阻塞 I/O)
- javascript - Jest 无法加载 svg 文件
- docker - 如何为 Docker 检索旧图像而不是最新图像?
- javascript - React Hooks 渲染两次