Emile Cantero
Emile Cantero

Reputation: 2053

Is it possible to include method in TypeScript interface?

Just a simple question I was thinking about, is it possible to include method or function in an interface as following:

Waiting for your comments and ideas about possibilities or issues doing this:

export interface INewsletter {
  id: number;
  title: string;
  release_date: any;
  filename: string;
  original_filename: string;
  notification: boolean;
  file: File;
  newsletterTranslations: any;
  translations: any;
  newsletterFiles: any;
  newsletter_files: any;

  myMethod() { something to do } // My method here
}

Upvotes: 0

Views: 289

Answers (1)

Igor
Igor

Reputation: 62213

An interface is a contract. You can specify the interface has a method but you can't include an implementation. So adding myMethod() : void; is valid but not myMethod() { something to do } as this includes a implementation/body.

export interface INewsletter {
  id: number;
  title: string;
  release_date: any;
  filename: string;
  original_filename: string;
  notification: boolean;
  file: File;
  newsletterTranslations: any;
  translations: any;
  newsletterFiles: any;
  newsletter_files: any;


  myMethod():void; // replace void with any other return type or any
}

Upvotes: 2

Related Questions