1

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.

roger
  • 1,225
  • 2
  • 17
  • 33

2 Answers2

4
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?

liviu blidar
  • 351
  • 4
  • 16
  • I made an update to my question, and I hope that will give you more about what I am trying to achieve. – roger May 28 '18 at 01:04
  • Let me know if my updated answer brings any joy. I have a feeling that the problem you're facing has some deeper roots in your code, more specifically a bad implementation of OOP principles. If some particular months are "special", but still hold the same class structure as a regular month but with the addition of having a "special" property as a boolean, you can opt for having the Month class as a parent of a SpecialMonth class that extends the Month class. See the following: https://stackoverflow.com/a/38834997/4183810 – liviu blidar May 28 '18 at 01:34
  • Thanks Liviu, i appreciate for you thoughtful answer and realise what I have suspected is true I inherit this code from someone, so I am trying to make it work somehow. – roger May 28 '18 at 01:38
0

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";
   ...
}
  • I thought that too, but as you may see in my code where I did the console, it printed `Month {}` and not `Month {January: undefined, ....}`. – roger May 28 '18 at 00:17
  • I looked at many sample codes and the way it was implemented in the typescript doc, they always have a constructor or a value passed to the class, like you need to fill in the definition before it exists. I hope that makes sense to you. – roger May 28 '18 at 00:19
  • 1
    @Kim.LBy maybe this could help: https://github.com/Microsoft/TypeScript/issues/8476 –  May 28 '18 at 00:25
  • Thanks @Jovan, just read your link and look like that is what I am looking for. Still feel fuzzy about what they are trying to achieve, may require second to understand this. – roger May 28 '18 at 01:20