DEV Community

Uzeyr OZ
Uzeyr OZ

Posted on

TypeScript for Functional Programmers

Functional programming is a programming paradigm that emphasizes the use of pure functions, immutability, and the avoidance of side effects. TypeScript, being a typed superset of JavaScript, provides a number of features that can help you write functional code.

One of the key features of functional programming is the use of pure functions. A pure function is a function that always returns the same output for a given input, without causing any side effects. In TypeScript, you can use type annotations to ensure that your functions are pure.

For example, here is a simple pure function that adds two numbers together:

function add(a: number, b: number): number {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Notice how the function takes in two parameters, both of which are annotated with the number type. The function also has a return type of number, indicating that it will always return a number.

Immutability is another important aspect of functional programming. In TypeScript, you can use the const keyword to create variables that cannot be reassigned. For example, here is a simple immutable object:

const person = {
  name: "John Smith",
  age: 30
};
Enter fullscreen mode Exit fullscreen mode

Because the person object is declared as a const, you cannot reassign it to a new object. However, you can still change the properties of the object, as long as you do it immutably. For example, you can create a new object that has the same properties as the original, but with an updated age:

const updatedPerson = {
  ...person,
  age: 31
};

Enter fullscreen mode Exit fullscreen mode

TypeScript also provide a nice syntax for functional programming such as

Higher-Order function
Partial application
-Currying
For example, here is a simple example of a higher-order function that takes a callback and applies it to each element of an array:

function map<T, U>(array: T[], callback: (item: T) => U): U[] {
  return array.map(callback);
}

Enter fullscreen mode Exit fullscreen mode

Here, the map function takes two generic types, T and U, and two parameters: an array of type T[] and a callback function that takes an item of type T and returns a value of type U. The function uses the built-in Array.prototype.map method to apply the callback to each element of the array, and returns a new array of the same length but with elements of type U.

To use the map function, you can pass in an array of numbers and a callback that squares each number:

const numbers = [1, 2, 3, 4, 5];
const squares = map(numbers, (n) => n * n);
console.log(squares); // [1, 4, 9, 16, 25]

Enter fullscreen mode Exit fullscreen mode

In conclusion, TypeScript provides a number of features that can help you write functional code, such as type annotations, immutability, and higher-order functions. By using these features, you can write code that is more predictable, testable, and maintainable.

Top comments (0)