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.