2017-04-03 12 views
0

私は電子アプリで型の安全性を与えるイベントのオーバーライドをいくつか指定しようとしています。私のタイプについてインポートされたtypescriptモジュールのインターフェイスを拡張する

export type SectionName = 'webview' 
    | 'index' 
    | 'settings' 

export interface ShowSection { 
    key: SectionName, 
} 

私はこのコードを補強したい:

import { 
    ipcMain, 
} from 'electron' 

ipcMain.on('rendered', (event) => { 
    const sender = event.sender 

    event.sender.send('show-section', { 
    key: 'index', 
    }) 
}) 

送信者は、私は次のように強化しようとしたhere

を定義しているタイプElectron.WebContentsでありますこれは:

declare namespace Electron { 
    interface WebContents extends NodeJS.EventEmitter { 
    send(channel: 'show-section', arg: ShowSection): void; 
    } 
} 

個々のイベントで型の安全性を確保できるようにする方法がわかりません。 WebContentsインターフェースを増強する

おかげ

答えて

1

正しい構文は次のとおりです。この後

declare global { 
    namespace Electron { 
     interface WebContents extends NodeJS.EventEmitter { 
      send(channel: 'show-section', arg: ShowSection): void; 
     } 
    } 
} 

あなたはsend(...)上の過負荷のオプションが表示されます。

enter image description here

UPDATE

宣言ファイル(例:カスタムタイピング.tsts):

export interface ShowSection { 
    key: SectionName 
} 

export type SectionName = 
    'webview' 
    | 'index' 
    | 'settings' 

declare global { 
    namespace Electron { 
     interface WebContents extends NodeJS.EventEmitter { 
      send(channel: 'show-section', arg: ShowSection): void; 
     } 
    } 
} 
+0

ああありがとう。残念ながら、私が期待していた副作用は、argの正しい値を持っていなければエラーになりますが、私は可能であれば確信が持てません。 – Tal

+0

私は文書を読んだことがありますが、私のコードにこのような宣言をどこに置くべきかは分かりません。どこにファイルを置くことができますか、コンパイルオプションを設定するにはどうしたらいいですか? – Tal

+1

元の 'event.sender.send(...) 'の問題は、' 'any''を第2引数の型として受け入れることです。モジュールの補強については、このために特定のファイルを作成することができます(例:custom-typings.d.ts)。これに間違いがありますか? – Diullei

関連する問題