Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript overriding extended property within constructor

I'm having an issue with Typescript where I extend a class and override a property from the super, however the super class property is still read in the constructor when I instantiate the sub class. Please see the below example:

class Person {

    public type:string = 'Generic Person';

    public constructor() {
        console.log(this.type);
    }

}

class Clown extends Person {

    public type:string = 'Scary Clown';

}

var person = new Person(), // 'Generic Person'
    clown = new Clown(); // 'Generic Person'

console.log(person.type); // 'Generic Person'
console.log(clown.type); // 'Scary Clown'

My expected behaviour would be 'Scary Clown' when I instantiate an instance of Clown. Is there another way I can achieve this without passing the values into the constructor itself or having some sort of init method that I fire manually after instantiating?

Thanks in advance :)

like image 921
Brent Avatar asked Dec 29 '14 22:12

Brent


People also ask

How do you override properties in TypeScript?

Use the Omit utility type to override the type of an interface property, e.g. interface SpecificLocation extends Omit<Location, 'address'> {address: newType} . The Omit utility type constructs a new type by removing the specified keys from the existing type.

Can we override method in TypeScript?

When a method is marked with override , TypeScript will always make sure that a method with the same name exists in a the base class. This member cannot have an 'override' modifier because it is not declared in the base class 'SomeComponent'.

Can we have multiple constructors in TypeScript?

In TypeScript, we cannot define multiple constructors like other programming languages because it does not support multiple constructors.

Is multiple inheritance possible in TypeScript?

TypeScript supports only single inheritance and multilevel inheritance. It doesn't support multiple and hybrid inheritance.


1 Answers

Property initializers are inserted right at the top of the constructor before the manually entered body of the constructor. So

class Person {
    public type:string = 'Generic Person';
    public constructor() {
        console.log(this.type);
    }
}

Becomes

var Person = (function () {
    function Person() {
        this.type = 'Generic Person';
        // NOTE: You want a different value for `type`
        console.log(this.type);
    }
    return Person;
})();

As you can see there is no way to get a different type in the parent constructor body using a property initializer.

Alternatively don't use type and rely on built-in constructor property:

interface Function{name?:string;}

class Person {    
    public constructor() {
        console.log(this.constructor.name);
    }
}

class Clown extends Person {    
}

var person = new Person(), // 'Person'
    clown = new Clown(); // 'Clown'

console.log(person.constructor.name); // 'Person'
console.log(clown.constructor.name); // 'Clown'
like image 97
basarat Avatar answered Oct 06 '22 23:10

basarat