The interface ensures that all callers of functions that implement the interface supply the required arguments - data and toUpper.

Because TypeScript understands that JavaScript doesn't mind if you pass arguments that aren't used, it cleverly allows this in implementations.

Why is this okay? Because it means you can substitute any implementation of the interface without affecting calling code.

Example: You can substitute either IFormatter implementation and the code works.

interface IFormatter {
    (data: string, toUpper: boolean): string;
};

var upperCaseFormatter: IFormatter = function (data: string) {
    return data.toUpperCase();
}

var variableCaseFormatter: IFormatter = function (data: string, toUpper: boolean) {
    if (toUpper) {
        return data.toUpperCase();
    }

    return data.toLowerCase();
}

// Switch between these at will
//var formatter = upperCaseFormatter;
var formatter = variableCaseFormatter;

formatter("test", true);

If TypeScript didn't do this, your upperCaseFormatter would have to have to have a parameter called toUpper that wasn't used anywhere in the function - which makes the code less readable.

Answer from Fenton on Stack Overflow
🌐
typescriptlang.org
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
In TypeScript, we represent those through object types. ... In all three examples above, we’ve written functions that take objects that contain the property name (which must be a string) and age (which must be a number). We have cheat-sheets available for both type and interface, if you want ...
🌐
Medium
medium.com › @jeffbutsch › typescript-interface-functions-c691a108e3f1
Typescript Interface Functions. This is how to declare function types… | by Jeff Butsch | Medium
February 21, 2019 - This function accepts a string as an argument and returns a number. interface MyClassProps { onChange(name: string): number; } Suffix the property name with the Optional Operator ?. Put the ? after the property name and before the argument list. In TypeScript React all props are required unless you use the Optional Operator.
People also ask

What are the types of TypeScript interfaces?
While there aren’t different types of interfaces, they can be used in various different ways, such as function types, object types, optional properties, index signatures, and more.
🌐
prismic.io
prismic.io › blog › typescript-interfaces
TypeScript Interfaces: A Practical Guide with Code Examples
When should I use interfaces in TypeScript?
You’ll use interfaces in various places in TypeScript, but most commonly, when you want to define the structure of an object and ensure type safety.
🌐
prismic.io
prismic.io › blog › typescript-interfaces
TypeScript Interfaces: A Practical Guide with Code Examples
What is the main difference between types and interfaces in TypeScript?
While a type and interface are very similar and are often interchangeable, the primary difference is that an interface can be freely extended at any point while a type can’t. Read more on Types vs. Interfaces on the TS docs here.
🌐
prismic.io
prismic.io › blog › typescript-interfaces
TypeScript Interfaces: A Practical Guide with Code Examples
Top answer
1 of 2
126

The interface ensures that all callers of functions that implement the interface supply the required arguments - data and toUpper.

Because TypeScript understands that JavaScript doesn't mind if you pass arguments that aren't used, it cleverly allows this in implementations.

Why is this okay? Because it means you can substitute any implementation of the interface without affecting calling code.

Example: You can substitute either IFormatter implementation and the code works.

interface IFormatter {
    (data: string, toUpper: boolean): string;
};

var upperCaseFormatter: IFormatter = function (data: string) {
    return data.toUpperCase();
}

var variableCaseFormatter: IFormatter = function (data: string, toUpper: boolean) {
    if (toUpper) {
        return data.toUpperCase();
    }

    return data.toLowerCase();
}

// Switch between these at will
//var formatter = upperCaseFormatter;
var formatter = variableCaseFormatter;

formatter("test", true);

If TypeScript didn't do this, your upperCaseFormatter would have to have to have a parameter called toUpper that wasn't used anywhere in the function - which makes the code less readable.

2 of 2
0

The accepted answer is misleading, it may suggest to try and error additional arguments in production code with the sole purpose to cheat a type checking that does not exist in production.

To allow variants with different number of arguments for type checking you should state these variants as so-called overloads like

interface IFormatter {
    (data: string, toUpper: boolean): string;
    (data: string): string;
};

var upperCaseFormatter: IFormatter = function (data: string) {
    return data.toUpperCase();
}  

upperCaseFormatter("test"); // matches one-argument overload
upperCaseFormatter("test","false"); // matches two-arguments overload

The initial question why TypeScript does not do the same checks on defining functions as on executing functions keeps open. Lacking any official documentation you could speculate that there are some technical restrictions for TypeScript itself being written in TypeScript / JavaScript and wait if thins will change with TypeScript 7 being written in Go: https://github.com/microsoft/typescript-go?tab=readme-ov-file#typescript-7

🌐
LogRocket
blog.logrocket.com › home › understanding and using interfaces in typescript
Understanding and using interfaces in TypeScript - LogRocket Blog
October 17, 2024 - To define an interface in TypeScript, use the interface keyword followed by the interface name and its properties: interface User { id: number; name: string; email: string; age?: number; // Optional property } In this example, we’ve defined ...
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-interface
TypeScript Interfaces
This means that any object of type IEmployee must define the two properties and two methods. Interface in TypeScript can be used to define a type and also to implement it in the class.
🌐
Contentful
contentful.com › blog › typescript-interfaces
TypeScript interfaces: How-to guide, best practices, and code examples | Contentful
April 23, 2025 - The syntax is similar to defining a regular TypeScript interface: ... The function interface example above specifies that a conforming function must accept a sound parameter (string value) and a count parameter (numeric value). It should not return a value (void).
🌐
Prismic
prismic.io › blog › typescript-interfaces
TypeScript Interfaces: A Practical Guide with Code Examples
September 28, 2023 - Interfaces are a feature of TypeScript ... has or should have. Their primary function is type checking and aiding developers in catching type-related errors during development....
Find elsewhere
Top answer
1 of 1
53

1.) There is a difference between method and function property declaration:

interface InterfaceA {
  doSomething(data: object): boolean; // method declaration
}

interface InterfaceB {
  doSomething: (data: object) => boolean; // function as property declaration
}

2.) TypeScript 2.6 introduces a compiler flag for stronger-typed, sound function types:

Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly. The stricter checking applies to all function types, except those originating in method or constructor declarations. (my emphasis)

So in general that is a good thing. In your example, InterfaceB has following contract: "Every function that can deal with a general object is compatible". But you want to assign a function doIt, that expects specific objects of type { type: string; } as input. A client that uses InterfaceB thinks, it is enough to pass object, but the implementation doIt wants something more concrete, so you rightfully get that error.

And why doesn't the error happen with InterfaceA ?

In contrast, methods like doIt in InterfaceA are excluded from --strictFunctionTypes for practical reasons. The developers decided the type system to be not too pendantic with built-in methods of Array etc. to have a reasonable balance between correctness and productivity.

So, in favor of stronger types, I would prefer the following type, which works for your case (sample):

interface InterfaceB {
  doSomething: ((data: { type: string; }) => boolean) | RegExp;
}
🌐
Educative
educative.io › blog › typescript-interfaces
A Simple Guide to Typescript Interfaces: declaration & use cases
March 10, 2026 - A TypeScript interface is an abstract type that defines the shape of an object by specifying property names, value types, and method signatures the compiler will enforce. Interfaces enable strong type checking at compile time, helping you catch errors early and keep your codebase predictable ...
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-generic-interface
Generic Interface in TypeScript
As you can see, we declared a function type numKVProcessor as let numKVProcessor: KeyValueProcessor<number, number> = processKeyPairs;. The type number will be used as the underlaying type of generic functionprocessKeyPairs. This will remove the need of defining separate functions for different data types. The generic interface can also be implemented in the class, same as the non-generic interface, as shown below.
🌐
Software Testing Help
softwaretestinghelp.com › home › javascript › typescript interface tutorial with examples
TypeScript Interface Tutorial With Examples
April 1, 2025 - In many cases, we find ourselves in dire need to define functions inside our TypeScript Interfaces, and so it is completely possible. Let us proceed and create a new function inside our Interface User, for example, we have a function with the name to get the message.
🌐
TutorialsPoint
tutorialspoint.com › typescript › typescript_interfaces.htm
TypeScript - Interfaces
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.
🌐
Cynoteck
cynoteck.com › blog-post › interfaces-in-typescript
Interfaces in Typescript: What Are They And How to Use Them?
November 19, 2025 - In TypeScript, we can work with “Interfaces”. In TypeScript, an interface just contains the definition of methods and properties, not their implementation. It is the functionality of the class that carries out the connection between the ...
🌐
freeCodeCamp
freecodecamp.org › news › how-typescript-interfaces-work
How TypeScript Interfaces Work – Explained with Examples
February 23, 2024 - At its core, an interface in TypeScript is a syntactical contract that defines the expected structure of an object. It provides a way to describe the shape of objects, including their properties and methods, without implementing any functionality.
🌐
Graphite
graphite.com › guides › typescript-interfaces
Understanding interfaces in TypeScript - Graphite
They are purely for declaration purposes, which means that they are used by TypeScript only at compile time and do not result in any JavaScript output. ... This Person interface requires any object that conforms to it to have both a name and an age property, both of which are strings. While both interface and type can be used to define the structure of an object or a function signature, they have some differences:
Top answer
1 of 2
4

In TypeScript, both interface and type can be used to describe complex types, including function types. However, they have some differences and use cases that may make one more suitable than the other in certain scenarios.

Interfaces in TypeScript define a contract that an object must adhere to. They are great for describing the shape of objects, and they can also describe function types. When you define a function using an interface, you're essentially defining a function signature that any function (that claims to implement this interface) must adhere to. This is useful, for instance, when you want to ensure that a function passed as a callback or a function stored in an object adheres to a specific.

In summary, whether to use an interface or a type alias to declare a function type in TypeScript depends on your specific needs. If you want to take advantage of features like class implementation and declaration merging, you might want to use an interface. But if you need more flexibility in representing various kinds of types, a type alias might be a better choice.

2 of 2
2

Using an interface, it ensures that the objects or functions you work with provide better type checking when it's annotated. In your case, the interface can be implemented this way:

interface SearchFunc {
  (source: string,
  subString: string): void;
}

The interface can be annotated this way:

const Search: SearchFunc = (source, subString) => {
  // Your code goes here
};

For your reference:

Typescript Function Interface

🌐
Scaler
scaler.com › home › topics › typescript › function interface in typescript
Function Interface in TypeScript - Scaler Topics
May 4, 2023 - Duck typing and structural subtyping are other terms for this process. A function or arrow function can be used to declare methods and attributes in an interface, which is specified by the keyword interface.
🌐
Index.dev
index.dev › blog › typescript-interfaces-as-function-arguments
How to Use Interfaces as Function Arguments in TypeScript
December 24, 2024 - An interface in TypeScript is a mechanism to specify the geometry of an object. It functions as a contract, specifying the attributes and methods an object should have. Interfaces do not include any implementation; they only describe the structure.
🌐
Strapi
strapi.io › blog › typescript-interfaces
What Are Typescript Interfaces and How Do They Work?
Interfaces can be extended, combined, ... adaptable to various scenarios · TypeScript interfaces are powerful tools for defining the shape of objects in your code....
🌐
TypeScript Tutorial
typescripttutorial.net › home › typescript tutorial › typescript interface
TypeScript Interface
July 19, 2024 - let lowerCase: StringFormat; lowerCase = function (str: string) { return str.toLowerCase(); } console.log(lowerCase('Hi', false));Code language: TypeScript (typescript) Notice that the second argument is passed when the lowerCase() function is called. If you have worked with Java or C#, you can find that the main use of the interface is to define a contract between classes.