In Typescript I have a class Contact
which implements an interface IContact
.
I want to add a property or function to the Contact
class which returns the combination of two other fields (firstname and lastname).
The definition of the IContact
interface looks like this:
export interface IContact {
firstname: string;
lastname: string;
}
The Contact
class look like this:
export class Contact implements IContact {
constructor(
public firstname: string,
public lastname: string
) { }
public fullname(): string {
return this.firstname + " " + this.lastname;
}
}
In my view I want to output the result of Contact.fullname()
but I receive an error that fullname
is not a function.
I do have access to all other properties on that class.
==== UPDATE ===
I'll add some extra code to clarify some stuff.
When I output the fullname
property in my view I have tried contact.fullname()
but also contact.fullname
which results in nothing.
In my component, trying to figure out what was happening I tried to output fullname
to the console like this: console.log("FULLNAME " +contact.fullname());
, but that give me the following message in the console: EXCEPTION _this.contact.fullname is not a function
===== UPDATED WITH ANSWER ========
As Sakuto stated correctly the list of contacts is created by some json received from the server. By creating a new instance entirely by calling it's constructor i was able to output the fullname
property.
Thanks Sakuto!