roger
roger

Reputation: 1263

Typescript: declare empty class object

Is it possible to declare an object from a class with no default values?

i have a class that looks like below;

export class Month {
 January: string;
 February: string;
 March: string;
 ...
}

mapMyPayload(specialMonths: Birthdays) {
 let myMonth = new Month;
 console.log(myMonth); //=> Month {}
    // here I expect the Object.keys(myMonth) 
    // returns {January=undefined, February=undefined, ...}
    // since I assume the new Month 
    // declares the myMonth as an object with undefined as its value
 Object.keys(myMonth).forEach(m => {
    // iterate thru the keys 
    // check if the month exists inside the specialMonths
    // and is not null or undefined
  if (specialMonths[m] != null) { 
   myMonth[m] = specialMonths[m];
 );
 return myMonth;
}

What I am trying to achieve is checking for any undefined or null in object and return this class.

I looked at many sample codes and documentation, either you have implicit constructor or explicit constructor you can declare you new class by using the new infront of the class name, but that follows by they declare some values. So I think the instances do not exist before it is declared by some outside scope.

Upvotes: 1

Views: 9996

Answers (2)

liviu blidar
liviu blidar

Reputation: 361

class Foo {
  a: string;
  b: string;
  c: number;
}

var foo = new Foo();
console.log(foo.a); //undefined

update: This transpiles to the following JS code:

var Foo = /** @class */ (function () {
    function Foo() {
    }
    return Foo;
}());
var foo = new Foo();
console.log(foo.a);

Your object doesn't have any keys because they're not yet defined, so they resort to the "undefined" literal as opposed to the "undefined" as a value. You can try doing something in the lines of defaulting to the "undefined" as value:

class Foo {
  a: string = undefined;
  b: string = undefined;
  c: number = undefined;
}

var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(property => console.log(property));
// keys log as empty and not undefined

this transpiles to the following JS:

var Foo = /** @class */ (function () {
    function Foo() {
        this.a = undefined;
        this.b = undefined;
        this.c = undefined;
    }
    return Foo;
}());
var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(function (property) { return console.log(property); });
// keys log as empty and not undefined as part of the forEach loop

I would personally advise against this implementation as it is prone to unexpected behaviour and goes against the statically typed version of JS that typescript brings along.

Perhaps if you would give a bit more detail on what you're trying to achieve we could give you a better solution?

Upvotes: 4

user8666203
user8666203

Reputation:

Why wouldn't be possible?

Fields and properties within a class will be set to default values, in your case null for string, if you don't specify their default values like this:

export class Month {
   January: string = "Something";
   February: string = "Something";
   March: string = "Something";
   ...
}

Upvotes: 0

Related Questions