2016-05-18 7 views
0

私のIonic2プロジェクトで、次のコードでNumberに新しいメソッドを追加しようとしています。私はイオン性のビルドを行うとionic2 typescript StringまたはNumberにメソッドを追加する際のコンパイルエラー

interface StringConstructor { 
    bar(msg: string): void; 
} 

String.bar = function(msg: string) { 
    alert("Example of static extension: " + msg); 
} 

String.bar("Hello World"); 


interface NumberConstructor { 
    toRad(): number; 
} 

Number.toRad = function() { return this * (Math.PI/180); }; 

export function gpsDist() { 

} 

は、それは私に次のエラーを与える:同じファイルがOK外のイオンをコンパイルしかし

TypeScript error: /Users/cju/Projects/Bus2-App/app/util/mathUtil.ts(7,8): Error TS2339: Property 'bar' does not exist on type 'StringConstructor'.

TypeScript error: /Users/cju/Projects/Bus2-App/app/util/mathUtil.ts(11,8): Error TS2339: Property 'bar' does not exist on type 'StringConstructor'.

TypeScript error: /Users/cju/Projects/Bus2-App/app/util/mathUtil.ts(18,8): Error TS2339: Property 'toRad' does not exist on type 'NumberConstructor'.

。私がtscでコンパイルすると、エラーは発生しません。すべてのコードは1つのファイルにあります。

+0

私は「ノードtsc.js myfile.ts」と「ノードtypescriptですを行うことによって、「イオンビルド」コマンドの外のファイルをコンパイルしようとしたが.js myfile.ts "、どちらもエラーなしでコンパイルします。 tsc.jsとtypescript.jsはionic-gulp-browserify-typescript/node_modules/tsify/node_modules/typescript/libにあります。 –

答えて

0

Angularはtsコンパイルを考慮して、アプリケーションのルートにあるtsconfig.jsonファイルのtypescriptコンパイラオプションを編集できます。しかし、私はあなたがそれを台無しにしたいとは思わない。

あなたがここに見つけることができる機能を拡張するための非静的な方法試してみてください:https://stackoverflow.com/a/13897813/1016128

+0

私はこれをしました。 インターフェイス番号{toRad():number; } Number.prototype.toRad = function(){これを返す* 2; }; それでも、「エラーTS2339:プロパティ 'toRad'がタイプ 'Number'に存在しません」というエラーが表示されます。しかし、それはまだ実行され、私に正しい結果を与える。 –

関連する問題