Function implementation is missing or not immediately following the declaration

avatar

Borislav Hadzhiev

Thu Mar 24 20222 min read

banner

Photo by Clay Banks

Function implementation is missing or not immediately following the declaration #

The error "Function implementation is missing or not immediately following the declaration" occurs when we declare a function without an implementation or make a syntax error. To solve the error, provide an implementation for the function and fix any syntax errors in your code.

function implementation missing

Here are 3 examples of how the error occurs.

index.ts
class Person { // ⛔️ Error: Function implementation is missing or not immediately following the declaration.ts(2391) console.log('Hello world') } // ⛔️ Error: Function implementation is missing or not immediately following the declaration.ts(2391) function sum(a: number, b: number): void; abstract class Employee { abstract salary: number; // ⛔️ Error: Function implementation is missing or not immediately following the declaration.ts(2391) increaseSalary(): number; }
The issue in the first example is that we are writing code directly in the body of a class, without declaring a method first.

To solve the error in this scenario, we have to declare a method in the class and move our code inside of the method.

index.ts
class Person { constructor(public name: string, public age: number) { console.log('hello world'); } someMethod() { console.log('hello world'); } } const p = new Person('James', 30) p.someMethod();

Moving our code into a class method solved the error.

The implementation of the function in the second example is missing.

index.ts
// ⛔️ Error: Function implementation is missing or not immediately following the declaration.ts(2391) function sum(a: number, b: number): number;

To solve the error we can declare a type alias when typing a function and separately declare the implementation.

index.ts
type Sum = (a: number, b: number) => number; const sum: Sum = (a: number, b: number) => { return a + b; };

We used a type alias to type the sum function.

The error is solved, because we provided an implementation for the function right after its declaration.

You might also get the error if you declare a method without an implementation in an abstract class.

index.ts
abstract class Employee { abstract salary: number; // ⛔️ Error: Function implementation is missing or not immediately following the declaration.ts(2391) increaseSalary(): number; }

To solve the error in this situation, we either have to prefix the method with the abstract keyword or provide an implementation for the method.

index.ts
abstract class Employee { abstract salary: number; // ✅ this works abstract increaseSalary(): number; }

Alternatively, you can provide an implementation for the method.

index.ts
abstract class Employee { abstract salary: number; increaseSalary(): number { return this.salary + 100; } }

Abstract classes in TypeScript:

  • can't be used to create an object directly - you can't use the new keyword to directly instantiate an abstract class
  • can only be used as parent classes
  • can contain real implementation for methods and properties, where the implemented methods can refer to abstract properties/methods
  • can make child classes promise to implement abstract methods/properties defined on the parent
Use the search field on my Home Page to filter through my more than 1,000 articles.