与 JavaScript 相比,TypeScript 提供了哪些额外的函数式编程功能?

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

我有兴趣学习 TypeScript,但前提是我可以使用它进行功能性编码,并尽可能避免命令式/OOP。

TypeScript 是否在基础 JavaScript 的基础上添加了更好地支持函数式编程的功能?它们是什么?

JavaScript 是一种多范式编程语言。

来自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 是 JavaScript 的超集,这意味着每个 JavaScript 程序也是一个有效的 TypeScript 程序。所以 TypeScript 也是一种多范式编程语言,可以用作函数式编程语言。

您可以通过 Michael Fogus 的 Functional JavaScript 一书学习如何使用 JavaScript 和 TypeScript 作为函数式编程语言。

另请查看一些开源库:

更新

我不认为 TypeScript 比基本 JavaScript 有任何额外的 FP 特性。但是,TypeScript 包含另一种函数语法,称为 lambda 语法 ()=>{}

我相信此语法并未添加到 TypeScript 中 "make the language more functional" 而是为了解决一个常见的 JavaScript 问题:dealing with the value of this.

我们可以争辩说,lambda 语法有助于创建看起来比传统 JavaScript 代码更实用的代码。例如,而不是写:

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

我们可以这样写:

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

或者只是:

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

更新 2

TypeScript 也有类型,类型在函数式编程中非常重要。阅读 André Staltz 的 "Is your JavaScript function actually pure?" 以了解原因。

打字稿 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.

代数数据类型是函数式编程语言的另一个重要特征。所以我们可以看到 TypeScript 开始为 FP 开发者添加功能。

更新 3

TypeScript 的最后两个版本引入了以下功能:结构类型系统、文字类型、可区分的联合、映射类型等。因此,类型系统现在对函数式编程用例的支持要好得多。