Alternatives to TypeScript

Rate this content
Bookmark

While TypeScript is currently the most popular static type checker for JavaScript, there are alternatives. In this talk, we'll look at Flow.js and Hegel, and review the similarities and differences among the three. Doing this will put TypeScript in perspective, and maybe help explain why the TS team made certain design decisions.

This talk has been presented at TypeScript Congress 2022, check out the latest edition of this JavaScript Conference.

FAQ

Ashley Claymore is a software engineer at Bloomberg who has been using TypeScript for six years. She has contributed to TypeScript and is a TC39 delegate.

Flow and Hagel use a colon for generic type constraints and have a prefix union question mark operator. They handle utility types directly in their compilers, whereas TypeScript uses aliases for type manipulation. Additionally, Flow and Hagel are considered to offer a safer interpretation of types compared to TypeScript.

Flow and Hagel treat object literals as closed by default, meaning any additional properties not specified in the type will cause an error unless explicitly declared as an open type using '...'. TypeScript does not enforce this by default.

Flow and Hagel have a more sound interpretation of types, making finer distinctions between types. They also allow for type inference from function call sites, which can influence the inferred types of other calls in the same file.

Hagel builds up a generic signature based on the function implementation for type checking, focusing on safety and minimizing the need for explicit type annotations. TypeScript, on the other hand, offers type inference as a quick fix rather than integrating it deeply into the core type-checking process.

Exploring alternatives such as Flow and Hagel can provide TypeScript developers with a broader understanding of different type-checking approaches and safety measures, which can enhance their programming practices and insights into TypeScript itself.

Some alternatives to TypeScript mentioned in the talk include Clojure, Flow, and Hagel.

Clojure is recognized primarily for its compiler that optimizes JavaScript effectively. It is used in projects like React, combined with Flow for type checking.

Ashley Claymore
Ashley Claymore
8 min
29 Apr, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
This Talk explores alternatives to TypeScript such as Clojure, Flow, and Hagel, highlighting their unique features and syntax. Flow and Hagel make finer distinctions between types and prioritize safety. TypeScript offers quick fixes for inferring types. Hagel aims for fewer type annotations and increased safety. These alternatives can provide insights and understanding of TypeScript.
Available in Español: Alternativas a TypeScript

1. Introduction to Alternatives

Short description:

Hello, and welcome to my Talk, Alternatives to TypeScript. I've been using TypeScript for six years now and have contributed to it as well. Today, I'll be discussing some alternatives like Clojure, Flow, and Hagel. Clojure is known for optimizing JavaScript, while Flow and Hagel have their own unique features and syntax compared to TypeScript. For example, Flow and Hagel use a colon for generic type constraints, have a prefix union question mark operator, and allow parameter names in function types. They also have their own utility types, while TypeScript uses aliases for type manipulation. Additionally, Hagel doesn't have an any type and uses the unknown type instead. Flow has both any and mixed types, while TypeScript has void and undefined types. Flow and Hagel also make finer distinctions between types, such as primitives, arrays, functions, and object literal types. Let's dive into the details!

Hello, and welcome to my Talk, Alternatives to TypeScript. While I'm a big fan of TypeScript, no language exists in a vacuum. They all influence and inspire each other. Who am I? My name is Ashley Claymore and I work as a software engineer at Bloomberg. I've been using TypeScript for six years now, meaning I've got to see how much it's evolved. I've also contributed to TypeScript and I'm a TC39 delegate.

So what are some of the alternatives? We have Clojure, we have Flow and we also have Hagel. People might be more familiar with the Clojure compiler as a JavaScript optimizer, and that's because it does really good job of optimizing JavaScript. Projects like React, which use Flow as the type checker, they then also use Clojure to optimize the code. Clojure does also have a type checker and it supports either JS doc or this more minimal start of type comment that I'm showing here. As far as I could tell, there isn't a Clojure language server that gets the errors to show up interactively, instead you run the compiler more like a traditional compiler. Unfortunately I only have a few minutes, I'm not going to have time to dig further into Clojure but if you're interested, please do go check it out.

Okay, there's a big block of code here and don't worry about trying to read it. The important thing I'm trying to show is that in this whole snippet, it's syntactically valid in all three, Hagel, Flow and TypeScript. This tells us they all have a similar look and feel to them, but they do diverge and for the rest of this talk, what I'm going to do is just going to give you a flavor of how they are different to each other. So for example, in Flow and Hagel, they're generic type constraints. They use a colon instead of an extends keyword. They also have this prefix union question mark operator, and they also allow parameter names to be emitted in function types. Flow primarily and Hagel exclusively use their built-in utility types when you're doing type manipulation instead of having some extra additional dedicated syntax for doing that. And then, these utility types are implemented directly in their compilers. If you compare that to the TypeScript approach, where almost all of the utility types are aliases for the syntactic way of doing that type manipulation, things like map types and conditional type syntax. If I create a type hierarchy diagram, that lets us compare a few differences in how the systems model their types. One time that Hagel is taking a different approach is that it doesn't have an any type. The closest you can get is to the safer unknown type. Flow does have any, and their unknown type, they call that mixed instead. And Flow also calls their bottom type empty instead of never. Where TypeScript has both a void and an undefined type, Flow only has void and Hagel only has undefined. TypeScript also put their object literal type much higher up, up above the primitives, whereas Flow and Hagel make this finer distinction between what's a primitive, array, a function, and what object literal types describe. I have an example of that coming up. Firstly, Flow and Hagel have this concept that types can be closed by default.

2. Type Checking and Safety

Short description:

In this example, if additional properties are passed in, it's treated as an error. To allow additional properties, add dot, dot, dot to the type. Flow and Hagel have finer type distinctions. They flow types from call sites, even without annotations. Hagle checks functions by looking at their implementation. TypeScript offers quick fixes for inferring types. Flow and Hagle strive for safety, making subtle distinctions. Hagle's goal is fewer type annotations and increased safety. It infers fixed-length arrays. Other tools' approaches can help understand TypeScript.

So, in this example, because I only mentioned the path property, when values are passed in that have additional properties, that's treated as an error, even if it's not done inline. If you're okay with having additional properties passed in, you need to add dot, dot, dot to the type. That tells us it's now an open type.

This is an example that's coming up. It shows how Flow and Hagel treat their object literal type I'm passing a string and trying to pass an array to a function where I said give me something with a length property. As JavaScript developers, we know these things, you can access a length property, but that's getting an error here. That's because of that finer distinction between these types. If we need to include them, then I have to explicitly say all string or array or function to say I expect these types to also be okay.

One of the big selling points is that it will flow the types being passed into a function from the call sites. That allows it to check code even if it doesn't have a type annotation. One thing to be aware of with this is that when you add a new call to a function is that that impacts the influence of the same calls to that function in the same file. So here where it's previously inferring a string return type, that's now inferring a string or number because I've introduced a new call that's passing in a different type. Hagle also had a name to be able to check functions even if they don't have type annotations but they took a different approach. Instead, looking at the function implementation, it built up a generic signature that can be reused. Another thing I saw here is that Hagle won't allow it to implicitly infer conversion from number to string. Instead, it must be explicit. TypeScript took a different approach. While it can infer the types for functions without annotations, instead, that's offered as a quick fix instead of using that logic as part of the core type checking. And a general theme that I felt when I was looking at these languages, it seems that Flow tries to be safer than TypeScript, and then Hagle again tries to be more safe than Flow. And Flow and Hagle's more sound interpretation of types can have it can be really subtle. For example, in the type I'm showing here, Flow and Hagle make the technically more sound distinction between these two types, whereas in many situations, TypeScript allows you to use them interchangeably because in many cases, it is okay to use them interchangeably. And then Hagle's goal of not requiring as many type annotations combined with this increased emphasis and focus on safety, it results in a different feel to the JavaScript you write. For example, array literals are inferred as a fixed length double, no need for as const. Hagle also reports an error that I'm not using the return value of push, the new length. I have to use a void expression to make it explicit that I'm doing this for a side effect and not interested in the return value.

Okay. That was a really lightning-fast exploration of these other tools. I hope the one thing I've shown is even though they all start with this same underlying challenge of statically type checking JavaScript, they all have their unique and equally valid approaches. I find that really fascinating. I feel it demonstrates how much creativity pours into these tools. For me, personally, looking at those other tools, helped me better understand TypeScript. So, for people that are TypeScript developers that aren't using these tools, I still think it's worth checking them out. I hope you'll find it interesting as well.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
TypeScript and React: Secrets of a Happy Marriage
React Advanced 2022React Advanced 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.

Workshops on related topic

React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Top Content
Featured Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced 2022React Advanced 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side.