ccnokes ccnokes - 1 year ago 91
TypeScript Question

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

I'm using TypeScript with Electron and I'd like to use the type definition for

. The
class is not exposed directly in the API, so this doesn't compile:

// 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 => === id).pop();

But I can see the interface defined in the typing file:

How do I use that type without being able to directly import the

Versions and 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"

Answer Source

Just as most declaration files on DT, this one is a global declaration file. This means that you don't have to import anything, the type declarations are always present anywhere in your project. Eg. this:

// app.ts
let theWebContents: Electron.WebContents

should compile just fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download