gpt4 book ai didi

javascript - 如何使用库未公开但在类型中实现的类型定义?

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:45 25 4
gpt4 key购买 nike

我在 Electron 中使用 TypeScript,我想使用 WebContents 的类型定义。 WebContents 类未直接在 API 中公开,因此无法编译:

// I'd like to set the return type to WebContents, but TypeScript can't 
// find that type
import { webContents } from 'electron';
export function getWebContents(id: number) : WebContents {
return webContents.getAllWebContents().filter(wc => wc.id === id).pop();
}

但是我可以看到打字文件中定义的接口(interface):https://github.com/DefinitelyTyped/DefinitelyTyped/blob/00b54a3f25f111afd7a6b70b3b56091c5f434129/github-electron/github-electron.d.ts#L3454

如何在无法直接导入 WebContents 类的情况下使用该类型?

版本和 typings.json:

"electron-prebuilt": "^1.2.8"
"typescript": "^1.8.10"
"typings": "^1.3.2"
----
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20160807145350",
"github-electron": "github:DefinitelyTyped/DefinitelyTyped/github-electron/github-electron.d.ts#00b54a3f25f111afd7a6b70b3b56091c5f434129"
}

最佳答案

与DT上的大多数声明文件一样,这个是全局声明文件。这意味着您无需导入任何内容,类型声明始终存在于项目的任何位置。例如。这个:

// app.ts
let theWebContents: Electron.WebContents

应该可以正常编译。

关于javascript - 如何使用库未公开但在类型中实现的类型定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com