Is it possible to implement a function interface in TypeScript?

typescript interface function
typescript interface array
typescript interface naming convention
typescript function type
typescript where to put interfaces
typescript optional property
typescript interface object
typescript interface constructor

I would like to be able to do this

class MyFunc extends ((s: string) => boolean) { ... }

so that an instance of MyFunc can be used as a function which takes a string as input and returns a boolean as follows:

const f = new MyFunc();
const b: boolean = f('someString');

Is this possible in TypeScript?

In languages such as Scala, one can extend the type String => Boolean, and provide an apply method to achieve this.

class MyFunc extends (String => Boolean)
val f = new MyFunc()
val b: Boolean = f("someString")

Perhaps you are thinking of something like this?

interface FunctionInterface {
    (s: string): boolean;
}

const f: FunctionInterface = s => true;
const b: boolean = f('someString');

Implementing TypeScript interface with bare function signature plus , A class cannot implement everything that is available in a typescript interface. Two prime examples are callable signatures and index  An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.

There is no default apply concept in TypeScript, but there are ways of creating typed objects that are also functions.

interface MyCallable {
    (param1: string, param2: number): string;

    prop1: string;
    prop2: number;
    extraMethod: (param1: boolean) => boolean;
}

function makeMyCallable(prop1: string, prop2: number): MyCallable {
    let that = ((param1: string, param2: number) => param1 + param2) as MyCallable;

    that.prop1 = prop1;
    that.prop2 = prop2;
    that.extraMethod = (param1: boolean) => !param1;

    return that;
}

let mc = makeMyCallable("3", 4);

mc("3", 4);
mc.prop1 = "string";
mc.prop2 = 5;
mc.extraMethod(false);

Interfaces · TypeScript, interface LabeledValue { label: string; } function printLabel(labeledObj: to printLabel implements this interface like we might have to in other languages. If the object we pass to the function meets the requirements listed, then it's allowed. The first answer to that question was discussed earlier and relates to consistency. Classes that implement an interface must implement all of the required members (note that TypeScript interfaces also support optional members as well). This makes it easy to enforce consistency across multiple TypeScript classes.

If you only have this function in your interface, you can define it like

type FuncType = (s: string) => boolean;

to use it like

const myFunc: FuncType;
function getFunction(): FuncType {}

TypeScript Interface, The getManagerName method is declared using a normal function. Interface in TypeScript can be used to define a type and also to implement it in the class. Or is it possible to implement it in TypeScript without having to implement the class using javascript? From a design perspective, it isn't usual to specify the constructor requirements in an interface.

If all you want is a simple function to accept a string and return a boolean, you don't need to use a class for this.

const myFunc = (s: string): boolean => {
  return s !== "";  //whatever logic is needed here
};

But, yes you can extend classes in TypeScript

class MyBaseClass {
  constructor() { }

  public doThing = (s: string): boolean => {
    return s !== "";  //whatever logic is needed here
  }
}

class MyFunc extends MyBaseClass {
  constructor() {
    super();
  }
}

const f = new MyFunc();
const b = f.doThing("hi"); //returns a boolean

Updated for response to comment

As mentioned below in another answer, you cannot really new up a class, assign that instance to a variable and then call it as a function. You could do that on the creation though, like this:

class MyBaseClass {
  constructor() { }

  public doThing = (s: string): boolean => {
    return s !== "";  //whatever logic is needed here
  }
}

class MyFunc extends MyBaseClass {
  constructor(private s: string) {
    super();
    this.doThing(this.s);
  }
}

const f = new MyFunc("hi"); //returns a boolean

You can play with the above code on the Typescript playground here

Interfaces in TypeScript: What are they and how do we use them , A class or function can implement an interface to define the implementation of the properties as defined in that interface. Let's write a function to  TypeScript can implement all four of them with its smaller and cleaner syntax. Read Write Object-Oriented JavaScript with TypeScript. Q9. Which object oriented terms are supported by TypeScript? Ans. TypeScript supports following object oriented terms. Modules; Classes; Interfaces; Data Types; Member functions; Q10.

Typescript Interface Functions - Jeff Butsch, This is how to declare function types within Typescript interfaces. Wrote this article because of confusing information out there and Typescript documentation is  If you extend T like <T extends DefaultTemplate> the typescript compiler could implement access to static methods or variables like this T.defaultTemplate. That would improve typescript. That would improve typescript.

TypeScript - Using Interfaces to describe Functions, In TypeScript, interfaces can also describe functions. In other words Output. there. Now let's create an interface describing above function:  To use Typescript to it’s fullest, you have to think your interfaces, your types (as in type Something = {}), think ahead of everything you need to write. You’ll end up creating a lot of those in order to achieve the nirvana that the transpiler wants.

How to Define an Interface in TypeScript, A developer provides a tutorial on how to work with the TypeScript language, It is the responsibility of the class that implements the interface by Let's call the toString() method of both the instances and observe how it prints the person detail in the screen: Type Safe Message Dispatch in TypeScript. The definition of a function as a subset of a cartesian product shows how in mathematics every function is pure: there is no action, no state mutation or elements being modified. In functional programming the implementation of functions has to follow as much as possible this ideal model.

Comments
  • @SalientBrain I've updated the answer to work in TS3.2.
  • Your second example of use is incorrect. You would be creating a function called getFunction that returns a function of FuncType, not a boolean.
  • That second example is returning a function returning a boolean, which would look funny if defined directly.
  • Yes, that's what I said... And it's not what the author wanted. The author wanted to define a type for a function that takes a string and returns a boolean. What you're doing on the second line is you're defining a function that returns a function. And that is confusing. A function cannot implement an interface, at least not in the current TypeScript version. By looking at the second line, someone might think it's possible.
  • Thanks for the quick answer. But you'd have to write f.doThing('str') to use f as a function, right? The point of extending that function class is for f to be used as a function directly (i.e., called like this: f('str'))