Greg Gum
Greg Gum

Reputation: 38109

How to declare a property as a subclass

I have a 3rd party lib that returns a BaseClass.

I then want to add a properties to BaseClass and use it. But I get a compile error.

var mySubclass : Subclass = MethodThatReturnsBaseClass();

Compile Error:Type Baseclass is not assignable to type Subclass

interface Subclass extends BaseClass {
title: string;
}

How should this be written?

Upvotes: 0

Views: 54

Answers (1)

basarat
basarat

Reputation: 276255

How should this be written?

One possibility:

interface Subclass extends BaseClass {
    title?: string;
}

Another:

var mySubclass : Subclass = <Subclass>MethodThatReturnsBaseClass();

I prefer method 1 as it is more semantically correct

Upvotes: 1

Related Questions