Joseph T F
Joseph T F

Reputation: 801

How can I declare a typescript interface as a class attribute

I have defined an interface like this:

export interface Donor{
    donorName: string;
    donorId: string;
    donorPassword:string
    donorAge: number
    fitnessReport: string
    physicianApproval: string
}

I would like to use a variable of this interface type as a private class attribute in the below class

class SawtoothService {

  //Donor component
  private currentDonor: <Donor>;

  public setDonor(currentDonor) {
    this.currentDonor = currentDonor;
   }

}

I will be setting it to a implementation done somewhere else by calling the function setDonor.

This is throwing error in the line

private currentDonor: <Donor>;

Upvotes: 2

Views: 57

Answers (1)

Mark Ormesher
Mark Ormesher

Reputation: 2408

To expand a little on the comment that gives the correct solution, the < and > characters are used for specifying generics*, for example if you had multiple donors you might use Array<Donor>.

In this case you just have a plain old Donor instance, so you don't need the triangle brackets. It should be the same format as your donorName: string line, expect the type here is Donor not string.

* They're also used for greater/less than comparison, of course, but I'm sure you know that already and it's not relevant here.

Upvotes: 1

Related Questions