Introduction to Rescript

This article introduces Rescript, a JavaScript dialect, highlighting its robust type system, immutable data structures, variant types, pattern matching, and integration with React.

ByteFE
ByteFE
ByteFE
Introduction to Rescript

rescript 介绍

rescript 跟 typescript 类似,也是一门 js 方言。在现在 typescript 大流行的背景下,为什么会写这篇文档去介绍 rescript 呢?

最初接触 rescript 的原因是惊讶于作者居然是国人大牛,感到十分钦佩。初步探索时发现这门语言本身有着很多亮眼的地方,比如更健壮的类型系统、更纯粹的函数式编程支持、强大的语言特性、原生语言编写的性能极致的编译器等等,当然也有着相应的劣势。本文会着重介绍 rescript 强大的特性,周边的生态以及和我们日常使用最紧密的 react 的结合。

语言特性

rescript 本身的语法不像 ts 是 js 的超集,和 js 是很不一样的,琐碎的语法就不详细介绍了,主要列举一些比较典型的特性来介绍。

Type Sound

type sound 的含义引用维基百科的一句介绍:

If a type system is sound , then expressions accepted by that type system must evaluate to a value of the appropriate type (rather than produce a value of some other, unrelated type or crash with a type error).

简单理解就是编译通过的类型系统在运行时不会产生类型错误,ts 就不是 type sound,原因可以看下面这个例子:

ByteFE
Written by

ByteFE

Cutting‑edge tech, article sharing, and practical insights from the ByteDance frontend team.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.