बस जोड़ने है कि अगर आप कुछ है कि पहले से ही घोषित की गई परिभाषित जोड़ने की कोशिश कर रहे हैं, तो यह ऐसा करने का typesafe रास्ता है, वह भी गाड़ी के खिलाफ की रक्षा करता है for inकार्यान्वयन।
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});
};
जो सुरक्षित रूप से polyfill के लिए इस्तेमाल किया जा सकता है। उदाहरण
//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);
}
दुर्भाग्य से यह एक की वजह से अपने प्रतीकों typecheck नहीं कर सकते वर्तमान टीएस में सीमा , और यह आप पर चिल्लाना नहीं होगा यदि स्ट्रिंग किसी भी परिभाषा से मेल नहीं खाता किसी कारण से, अगर वे पहले से कर रहे हैं मैं देखने के बाद बग रिपोर्ट करेंगे ध्यान रखें।