Problems extending Object.prototype with Node.js and /// <reference path="node/node.d.ts" />

Topics: General
Jun 24, 2014 at 4:10 PM
Edited Jun 24, 2014 at 4:16 PM
Hi,

I am having problems extending Object.prototype using interface in the presence of DefinitelyTyped definitions. The following code :-
/// <reference path="node/node.d.ts" />

import path = require('path');

interface Object {
    deepClone():Object;
}

function getFileExtension(filename:string) {
    return path.extname(filename).split(".")[1];
}

Object.prototype.deepClone = function() {
    var that = new Object();
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            if (typeof this[key] == "object")
                that[key] = this[key].deepClone();
            else
                that[key] = this[key]
        }
    }
    return that;
}
produces the following error :-
error TS2094: The property 'deepClone' does not exist on value of type 'Object'.
While removing the import, and leaving the node definition :-
/// <reference path="node/node.d.ts" />

//import path = require('path');

interface Object {
    deepClone():Object;
}

Object.prototype.deepClone = function() {
    var that = new Object();
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            if (typeof this[key] == "object")
                that[key] = this[key].deepClone();
            else
                that[key] = this[key]
        }
    }
    return that;
}

This compiles okay. I don't know whether this is a problem with TypeScript or the DefinitelyTyped definitions, but it looks like its a TypeScript problem as I could not find any other 'interface Object' definitions anywhere. It looks like as soon as modules and imports are brought into the equation that Object, becomes fixed, yet there are no error messages on my Objects interface definition, yet there are errors on the actual function definitions.

Aaron
Jun 24, 2014 at 4:14 PM
Edited Jun 24, 2014 at 4:16 PM
Hi,

I am having problems extending Object.prototype using interface in the presence of DefinitelyTyped definitions. The following code :-
/// <reference path="node/node.d.ts" />

import path = require('path');

interface Object {
    deepClone():Object;
}

function getFileExtension(filename:string) {
    return path.extname(filename).split(".")[1];
}

Object.prototype.deepClone = function() {
    var that = new Object();
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            if (typeof this[key] == "object")
                that[key] = this[key].deepClone();
            else
                that[key] = this[key]
        }
    }
    return that;
}
produces the following error :-
error TS2094: The property 'deepClone' does not exist on value of type 'Object'.
While removing the import, and leaving the node definition :-
/// <reference path="node/node.d.ts" />

//import path = require('path');

interface Object {
    deepClone():Object;
}

Object.prototype.deepClone = function() {
    var that = new Object();
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
          if (typeof this[key] == "object")
                that[key] = this[key].deepClone();
            else
                that[key] = this[key]
        }
    }
    return that;
}

This compiles okay. I don't know whether this is a problem with TypeScript or the DefinitelyTyped definitions, but it looks like its a TypeScript problem as I could not find any other 'interface Object' definitions anywhere. It looks like as soon as modules and imports are brought into the equation that Object, becomes fixed, yet there are no error messages on my Objects interface definition, yet there are errors on the actual function definitions.

Aaron
Jun 24, 2014 at 4:19 PM
Jun 25, 2014 at 4:38 PM
Moved to be an issue.