簡體   English   中英

如何使用庫未公開但在鍵入時實現的類型定義?

[英]How do I use a type definition that is not exposed by the library but is implemented in the typing?

我正在使用TypeScript和Electron,我想使用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();
}

但我可以看到輸入文件中定義的接口: 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

應該編譯得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM