2016-12-09 19 views
1

私はwebkitAudioContextのようなウィンドウオブジェクトに新しいタイプを定義しようとしています。私は、私は以下のように定義ファイルをインポートしています別のモジュールからは、window.d.tsと呼ばれ、その内側に、私は以下のコードを追加しましたウィンドウのタイスクリプト定義を宣言する正しい方法

/// <reference path="./window.d.ts" /> 

let contextClass = window.AudioContext || window.webkitAudioContext; 
let context = new contextClass(); 

interface Window { 
    AudioContext: Constructable; 
    webkitAudioContext: Constructable; 
} 

interface Constructable { 
    new(); 
} 

を別のファイルを作成しました

上記の2行はうまく動作します。

私はその後、それが働いていない、以下のように

declare module window { 
    export interface Window { 
     AudioContext: Constructable; 
     webkitAudioContext: Constructable; 
    } 

    interface Constructable { 
     new(); 
    } 
} 

をdefintionファイルを変更した場合。定義をウィンドウに定義する正しい方法は何ですか?

+0

ウィンドウ宣言を名前空間内に置くことはできませんが、デフォルトで提供されているWindow型の型とはもはや一致しません。 – toskv

答えて

1

AudioContextwebkitAudioContexthereのコミュニティ記述定義が既に存在します。 @typesに慣れていない人は、hereをもっと読むことができます。多くのライブラリの定義がたくさんあります(JQueryPromiseをタイプとして使用することさえあります)。
また、このようなコードが含まれていwhich'llカスタムdefinions、とglobal.d.tsファイル作成のgood practiceがあります:

declare var myCustomLib: any; 

次にあなたがtsconfig.jsonglobal.d.tsを追加する必要がありますので、コンパイラはあなたの定義を承知しているであろう:

は、
{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "target": "es5", 
    "outDir": "./build" 
    }, 
    "exclude": ["node_modules"], 
    "files": ["./src/globals"] 
} 

また、廃止予定の機能にアクセスしようとすると、次のような(... as any)構成でアクセスできます。

let Ctx = window.AudioContext || (window as any).webkitAudioContext; 

このテクニックのいくつかが役立つことを願っています。

関連する問題