Randomdev
Randomdev

Reputation: 33

Typescript- Creating member function outside of a Class definition with return types

How do you define a member function, outside the definition of a class in Typescript?

To quote an example from c++:

class A
{
   void runThis();
  //Member variables and constructors here 
} 


A::runThis()
{
  //code goes here
}

How do you achieve the same in typescript? By searching in google I found something like:

class A
{
  runThis: () => void;             
}


A.prototype.runThis = function(){
 //Anonymous function code goes here
}

However, I have not found the syntax, to do the same with functions that have a return type (such as a number or a string).

How can this be achieved?

Upvotes: 3

Views: 3824

Answers (2)

Wesley Ferreira
Wesley Ferreira

Reputation: 1

In TypeScript you can use static method for your problem.


Sample:

export class Person {

   private static birthday : Date;

   public static getBirthDay() : Date {
     return this.birthday;
   }
}

Person.getBirthDay();

Upvotes: 0

Saravana
Saravana

Reputation: 40642

You can definitely use the same syntax to add functions with return types:

class A {
  add: (a: number, b: number) => number;
}

A.prototype.add = (a: number, b: number): number => {
  return a + b;
}

Upvotes: 3

Related Questions