How does prototype extend on typescript?

后端 未结 4 789
情歌与酒
情歌与酒 2020-12-06 09:48

i extended function prototype but typescript doesn\'t recognize it.

Function.prototype.proc = function() {
  var args, target, v;
  var __slice = [].slice;
          


        
4条回答
  •  萌比男神i
    2020-12-06 10:38

    Just adding that if you're trying to add define something that's already declared, then this is the typesafe way of doing so, that also guards against buggy for in implementations.

    export const augment = (
        type  :new (...args :any[]) => T,
        name  :U,
        value :U extends string ? T[U] : any
    ) => {
        Object.defineProperty(type.prototype, name, {writable:true, enumerable:false, value});
    };
    

    Which can be used to safely polyfill. Example

    //IE doesn't have NodeList.forEach()
    if (!NodeList.prototype.forEach) {
        //this errors, we forgot about index & thisArg!
        const broken = function(this :NodeList, func :(node :Node, list :NodeList) => void) {
            for (const node of this) {
                func(node, this);
            }
        };
        augment(NodeList, 'forEach', broken);
    
        //better!
        const fixed = function(this :NodeList, func :(node :Node, index :number, list :NodeList) => void, thisArg :any) {
            let index = 0;
            for (const node of this) {
                func.call(thisArg, node, index++, this);
            }
        };
        augment(NodeList, 'forEach', fixed);
    }
    

    Unfortunately it can't typecheck your Symbols due to a limitation in current TS, and it won't yell at you if the string doesn't match any definition for some reason, I'll report the bug after seeing if they're already aware.

提交回复
热议问题