ccnokes ccnokes - 3 months ago 22
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

WebContents
. The
WebContents
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 => wc.id === id).pop();
}


But I can see the interface defined in the typing file: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/00b54a3f25f111afd7a6b70b3b56091c5f434129/github-electron/github-electron.d.ts#L3454

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

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

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.