【问题标题】:How do I create polyfill for build in types with TS如何使用 TS 为内置类型创建 polyfill
【发布时间】:2016-12-03 08:02:17
【问题描述】:

我有几个 polyfill,我在我的解决方案中使用它们。例如Array.includes:

 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;
  };
}

但我想将我的 utils.js 迁移到 utils.ts(我从不使用 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.includes 在 IE 中仍然是 undefined。当然,因为 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 本身?

【问题讨论】:

  • @MikeMcCaughan 感谢您提供链接。但是还有一个问题:例如here 被描述为Array 扩展,但没有关于扩展强类型(我的意思是Array&lt;T&gt; 或其他东西)
  • 这不是你的问题。只需搜索您的标题(或副本的标题),您就会获得大量点击。
  • @MikeMcCaughan 好吧,它回答了如何添加我的自定义方法。但还有另一个问题:如何添加 polyfill?例如我支持没有Array.includes 方法的IE9。所以我只应该在它当前不存在的情况下添加它。
  • String 没什么特别的。如果您在 typescript 中定义了自己的类,以后可以在不修改该类定义的情况下为其添加新方法吗?我知道的唯一方法是使用declaration merging 声明该方法,然后分别使用defineProperty 添加实现或修改其原型。语言中没有内置的方法可以一步完成。

标签: javascript typescript


【解决方案1】:

代替class MyArray&lt;T&gt; extends Array&lt;T&gt; { `Array.prototype.includes = /在此处插入你的函数/

更多

如果您需要by extending lib.d.ts,您还应该将includes 声明为Array 的成员

【讨论】:

  • 我为什么要创建lib.d.ts?合并接口还不够?
猜你喜欢
  • 2021-12-03
  • 2020-09-13
  • 2018-12-14
  • 2020-03-22
  • 1970-01-01
  • 2021-11-23
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多