2016-12-02 1 views
3

私は私のソリューションで使用しているいくつかのポリフィルを用意しています。たとえば、Array.includesビルドタイプのポリフィルをTSで作成する方法

if (![].includes) { 
    Array.prototype.includes = function(searchElement/*, fromIndex*/) { 
    'use strict'; 
    var O = Object(this); 
    var len = parseInt(O.length) || 0; 
    if (len === 0) { 
     return false; 
    } 
    var n = parseInt(arguments[1]) || 0; 
    var k; 
    if (n >= 0) { 
     k = n; 
    } else { 
     k = len + n; 
     if (k < 0) { 
     k = 0; 
     } 
    } 
    while (k < len) { 
     var currentElement = O[k]; 
     if (searchElement === currentElement || 
     (searchElement !== searchElement && currentElement !== currentElement) 
    ) { 
     return true; 
     } 
     k++; 
    } 
    return false; 
    }; 
} 

しかし、私は(私はそれを再生したい私は決してユーザーTS)私のutils.jsutils.tsにを移行したい、と私は、次の書き込み:まだ

interface Array<T> { 
    includes(searchElement: T, fromIndex?: number): boolean; 
} 

if (!Array.prototype.hasOwnProperty('includes')) { 
    class MyArray<T> extends Array<T> { 
     includes(searchElement: T, fromIndex?: number): boolean { 
      const obj = Object(this); 
      const len = parseInt(obj.length) || 0; 
      if (len === 0) { 
       return false; 
      } 
      const n = fromIndex || 0; 
      var k: number; 
      if (n >= 0) { 
       k = n; 
      } else { 
       k = len + n; 
       if (k < 0) { 
        k = 0; 
       } 
      } 
      while (k < len) { 
       const currentElement = obj[k]; 
       if (searchElement === currentElement || 
         (searchElement !== searchElement && currentElement !== currentElement) 
       ) { 
        return true; 
       } 
       k++; 
      } 
      return false; 
     } 
    } 
} 

Array.prototype.includesundefined IEで。 TSはなくネイティブArrayの私のカスタムクラスを拡張しているので、当然のことながら、:

var __extends = (this && this.__extends) || function (d, b) { 
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 
    function __() { this.constructor = d; } 
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
}; 
if (!Array.prototype.hasOwnProperty('includes')) { 
    var MyArray = (function (_super) { 
     __extends(MyArray, _super); 
     function MyArray() { 
      _super.apply(this, arguments); 
     } 
     MyArray.prototype.includes = function (searchElement, fromIndex) { 
      var obj = Object(this); 
      var len = parseInt(obj.length) || 0; 
      if (len === 0) { 
       return false; 
      } 
      var n = fromIndex || 0; 
      var k; 
      if (n >= 0) { 
       k = n; 
      } 
      else { 
       k = len + n; 
       if (k < 0) { 
        k = 0; 
       } 
      } 
      while (k < len) { 
       var currentElement = obj[k]; 
       if (searchElement === currentElement || 
        (searchElement !== searchElement && currentElement !== currentElement)) { 
        return true; 
       } 
       k++; 
      } 
      return false; 
     }; 
     return MyArray; 
    }(Array)); 
} 

にはどうすればArray自体が拡張していますか?

+1

[TypeScript:組み込み型を補完する]の可能な複製(0120-17753) –

+0

@MikeMcCaughanリンクありがとうございます。しかし、1つの疑問が残っています。例えば、[ここ](http://typescript.codeplex.com/workitem/4)は '配列'拡張ですが、強く型付けされた拡張については何もありません( 'Array ' –

+0

それはあなたの質問についてではありません。あなたのタイトル(または重複のタイトル)を検索するだけで、たくさんのヒットを得ることができます。 –

答えて

1

代わりのclass MyArray<T> extends Array<T> { `Array.prototype.includes =/ここにあなたの関数を挿入/

複数の操作を行い

あなたはby extending lib.d.ts

に必要がある場合も Arrayのメンバーとして includesを宣言する必要があります
+0

'lib.d.ts'をなぜ作りますか?インターフェイスをマージするだけでは不十分です。 –

関連する問題