Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which additional Functional Programming features does TypeScript offer compared to JavaScript? [closed]

Tags:

I'm interested in learning TypeScript, but only if I can code functionally with it, and avoid imperative / OOP as much as possible.

Does TypeScript add features over basic JavaScript that better support functional programming? What are they?

like image 744
Bellarmine Head Avatar asked May 08 '15 08:05

Bellarmine Head


People also ask

How does TypeScript differ from JavaScript?

TypeScript is an object-oriented programming language developed by Microsoft Corporation, whereas JavaScript is the programming language for the web. TypeScript is an open-source language to build large-scale web apps, whereas JavaScript is a server-side programming language that helps to develop interactive web pages.

What makes TypeScript better than JavaScript?

TypeScript always points out the compilation errors at the time of development (pre-compilation). Because of this getting runtime errors is less likely, whereas JavaScript is an interpreted language. TypeScript supports static/strong typing. This means that type correctness can be checked at compile time.

Does TypeScript support functional programming?

TypeScript is not a purely functional programming language but offers a lot of concepts that are in line with functional programming languages. Most of the developers are oblivious to these concepts in TypeScript.


Video Answer


1 Answers

JavaScript is a multi-paradigm programming language.

From MDN:

A multi-paradigm programming language is a programming language that supports more than one programming paradigm. The central idea of a multiparadigm language is to provide a framework in which programmers can work in a variety of styles, freely intermixing constructs from different paradigms. The design goal of such languages is to allow programmers to use the best tool for a job, admitting that a single paradigm cannot solve all problems in the easiest or most efficient way.

Supporting this view, JavaScript supports, or infact uses various styles. For example, its syntax follows a C language like structure, which is a procedural language, and at the same time JavaScript copies many names and naming conventions from an object-oriented programming language, Java, but the two languages are otherwise unrelated and have very different semantics. The key design principles within JavaScript are taken from the Self and Scheme programming languages.

TypeScript is a superset of JavaScript which means that every JavaScript program is also a valid TypeScript program. So TypeScript is also a multi-paradigm programming language an can be used as a functional programming language.

You can learn how to use JavaScript and TypeScript as a Functional programming language with the book Functional JavaScript by Michael Fogus.

Also check out some open-source libraries:

  • underscore.js
  • lodash.js
  • functional.js

Update

I don't think TypeScript has any additional FP features over basic JavaScript. However, TypeScript includes an alternative function syntax known as lambda syntax ()=>{}.

I believe this syntax wasn't added to TypeScript to "make the language more functional" but to solve a common JavaScript problem: dealing with the value of this.

We can argue that the lambda syntax facilitates the creation of code that looks more functional than traditional JavaScript code. For example, instead of writing:

function isBigEnough(value) {
  return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

We can write:

var isBigEnough = (value) => value >= 10;
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

Or just:

var filtered = [12, 5, 8, 130, 44].filter((value) => value >= 10);

Update 2

Also TypeScript has types and types are very important in functional programming. Read "Is your JavaScript function actually pure?" by André Staltz to understand why.

TypeScript 2.0 introduces tagged unions.

Tagged unions are an exciting new feature that brings functionality from languages like F#, Swift, Rust, and others to JavaScript, while embracing the way that people write JavaScript today. This feature is also called discriminated unions, disjoint unions, or algebraic data types.

Algebraic data types is another important feature in functional programming languages. So we can see that TypeScript is starting to add features for FP developers.

Update 3

The last two releases of TypeScript have introduced features like: Structural type system, literal types, discriminated unions, mapped types, etc. So the type system has now a much better support for functional programming use cases.

like image 140
Remo H. Jansen Avatar answered Sep 28 '22 12:09

Remo H. Jansen