2

Currently learning TypeScript. I have a class:

class ComicBookCharacter {
  protected secretIdentity?: string;
  alias: string;
  health: number;
  strength: number;


  constructor({ alias, health, strength, secretIdentity }) {
    this.alias = alias;
    this.health = health;
    this.strength = strength;
    this.secretIdentity = secretIdentity;
  }
}

class SuperHero extends ComicBookCharacter {
  getIdentity() {
    console.log(`${this.alias} secret name is ${this.secretIdentity}`)
  }
}

const superman = new ComicBookCharacter({
  alias: 'Superman',
  health: 300,
  strength: 60,
  secretIdentity: undefined,
});

I'm wondering if I have to pass undefined when creating an instance of a class if I made one of the properties optional (like the secretIdentity here);

Removing secretIdentity: undefined gets me a compiler error.

Edit:

After the solution that Shift 'N Tab gave below, I did some more research and you can also specify the constructor like this:

constructor(fields: Partial<ComicBookCharacter> & {
  mothersName?: string,
  secretIdentity?: string,
}) {
  Object.assign(this, fields);
}

And as Shift 'N Tab wrote in the comment below, do not forget to add "strictPropertyInitialization": true in tsconfig file

2 Answers 2

2

If you wanna make an object argument in the constructor you can do that by Object destructuring approach.

class ComicBookCharacter{
    secretIdentity?: string;
    alias: string = '';
    health: number = 0;
    strength: number = 0;

    constructor(character: { alias: string, health : number, strength : number, secretIdentity?: string }) {
        Object.assign(this, character);
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

But is there a way to do it when still passing an object to the constructor? I find it more clear when it comes to code readability.
Thanks a lot! That’s really helpful!
Just wanted to say once again that this is a really clean way to do this. One thing that pops up in my editor is that it complains the following: Property alias has no initializer and is not definitely assigned in the constructor. The same is with health and strenth
I also found that it can be done with character: Partial<ComicBookCharacter>, but then you have to specify those optional fields using & { secretIdentity?: string }. I added the edit to my original post so it can maybe help some people :)
a work around to your linting error is to make "strictPropertyInitialization": true, from your tsconfig or do it safe by assigning manually each property in the constructor class or make a default property value in each declaration.
|
0

I think you can set secret identity to an empty string in the constructor ( secretIdentity = ‘’ ).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.