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

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


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:

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.