Nasıl prototip typescript üzerinde uzanır mı?

oy
15

i fonksiyon prototipi uzatıldı ancak typescript tanımıyor.

Function.prototype.proc = function() {
  var args, target, v;
  var __slice = [].slice;
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  target = this;
  while (v = args.shift()) {
    target = target(v);
  }
  return target;
};
// generated by coffee-script

var foo: (number) => (string) => number
  = (a) => (b) => a * b.length;
console.log(foo.proc(first, second))

Sonuç: tsc -e

The property 'proc' does not exist on value of type 'Function'

nasıl ben bu nesneyi uzatılır?

Oluştur 07/10/2012 saat 05:27
kaynak kullanıcı
Diğer dillerde...                            


4 cevaplar

oy
33

Fonksiyon nesnelerin üyelerini beyan standart typescript lib bir işlev arayüzü yoktur. Kendi başına bu arabirim üyesi olarak proc beyan aşağıdaki gibi eklemek gerekir:

interface Function {
    proc(...args: any[]): any;
}

her yerde 'proc' kullanmak niyetinde gelen bu arayüz başvurulan gerekecektir.

Cevap 07/10/2012 saat 19:03
kaynak kullanıcı

oy
6

Bunun gibi:

declare global {
    interface Function {
        proc() : any;
    }
}

Olmadan çalışmıyor 'küresel beyan'.

Modül büyütme son daktilo versiyonunda sürümlerinde böyle işler. Check out belgeler ve aşağı kaydırarak Module augmentationbölümünde.

Cevap 25/06/2016 saat 05:00
kaynak kullanıcı

oy
0

Sadece zaten deklare büyük bir şeyi tanımlamak eklemeye çalışıyorsanız, o zaman bu da hatalı karşı korur bunu yaparken türgüvenli yolu olduğunu ekleyerek for inuygulamaları.

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

Hangi güvenle polyfill için kullanılabilir. Örnek

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

Ne yazık ki nedeniyle sizin Sembolleri typecheck edemez cari TS sınırlama ve dize eğer sen bağırma olmayacak herhangi tanımına uymadığını zaten eğer ben gördükten sonra hatayı bildirmek edeceğiz nedense farkında.

Cevap 26/03/2019 saat 02:25
kaynak kullanıcı

oy
0

Bu birçok kişi bu soruyu görüntülemek beri söz konusu örnekteki gibi prototipler ekleyerek karşı tavsiye etmek ekliyorum. aşağıdaki gibi ekleyin:

interface Function {
    proc(...args: any[]): any;
}

Object.defineProperty(Function.prototype, 'proc', { value: function(arg: any[]) {
    // Your function body
}});

Sebebi doğrudan prototip eklerseniz, bu sayılan alabilir olması durumunda fonksiyon olsun bitti sayılan bir örneği. for i in ... Şimdi bu blok (son zamanlarda başıma) size kontrol etmiyoruz bir kod olabilir, bu nedenle mümkün olduğunca güvenli kodunuzu tutmak en iyisidir.

Cevap 02/05/2017 saat 22:52
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more