Guess what, JavaScript is getting types!?

Guess what, JavaScript is getting types!?

Daily short news for you
  • Void - the name I've mentioned quite some time ago. From the time when continue.dev just emerged. It's similar to Cursor and Windsurf, and today they've released the Beta version and allowed everyone to download it.

    The strength of this is that it is open source, free, and uses free local models on your machine via Ollama or LM Studio... If you don't like it, you can plug in APIs from other providers. I just tried it out and found the suggestion capabilities and chat framework quite similar to Cursor, and it even has an Agent feature 👏. It's more stable than continue.dev (the last time I used it), and the only thing left to do is to choose a better model 🤤

    » Read more
  • Zed has recently introduced a new feature called Agent - similar to Agent in Cursor or Write in Windsurf, and they call it The Fastest AI Code Editor.

    It is indeed fast because Zed is written in Rust. However, their strategy seems to be shifting, focusing on AI instead of developing the currently limited extensions that cannot compete with VSCode 🥶

    Zed: The Fastest AI Code Editor

    » Read more
  • Right after the news that OpenAI reached an agreement to acquire Windsurf for $3 billion, today Cursor has offered 1 year of free Pro access for students. Chaaaaà 🤔

    OpenAI Reaches Agreement to Buy Startup Windsurf for $3 Billion

    Cursor for Students | Cursor - The AI Code Editor

    » Read more

The problem

One of the things I found impressive when I started working with JavaScript is that it doesn't have types (dynamically typed). That means you don't have to declare data types for everything you create. This makes coding faster and more comfortable. On the other hand, not specifying data types also leads to many bugs in debugging and maintaining the project later on.

In the midst of this, TypeScript was introduced, bringing "statically typed" to JavaScript. This means that now you can declare types for almost everything, avoiding many errors in coding and making the code much more readable. Essentially, TypeScript is a type helper for JavaScript, which then compiles the ts syntax into js for browsers, Node.js, etc. to understand and execute.

Recently, TC39 has been working on a plan called Type Annotations. This is a step towards allowing JavaScript to declare types. Specifically, the type syntax is "borrowed" from TypeScript. So what's going on? Is JavaScript trying to "venture" into becoming a typed language? Does this make the community angry as coding becomes more complex and restrictive? Let's find out in the article below!

TC39 and the process of adding features to JavaScript

First, let's talk about TC39 and how they develop new features for JavaScript.

Ecma TC39 is responsible for developing the ECMAScript programming language (JavaScript) and drafting technical specifications. TC39 operates by consensus and has complete authority to change technical specifications when deemed appropriate. However, there is a common process for making technical changes.

Changes go through several strict stages, starting from the initial idea to the final implementation. There are five stages: the initial idea stage and four subsequent stages. TC39 must pass and evaluate each stage.

Type Annotations is one of the efforts to bring JavaScript closer to being a typed language. It has entered stage 1, which means it has passed the initial round. In this stage, it needs to describe more of what needs to be done and identify potential challenges to move on to the next rounds. This stage may still be lengthy, but let's see what this specification brings us.

How do Type Annotations work?

This proposal aims to allow developers to add type annotations to their JavaScript code, so that type checking tools like TypeScript can still work properly. When running, the JavaScript tool ignores the data types, treating them as just annotations. Type Annotations are not intended to replace TypeScript or other type checking tools. They are intended to answer the question "How should JavaScript behave when types are present?"

Specifically, it allows developers to run programs written in TypeScript, Flow, and other type-checking tools for JavaScript without the need to transpile the code, with the condition that these type-checking languages must be a subset of the supported Type Annotation definitions.

For example:

let str:string = "hello world";
str = 5;

When running in a JavaScript environment, the code will still execute normally because JavaScript doesn't care about the type of str, so it can be assigned a number even though it was initially declared as a string. However, if compiled with TypeScript, an error will occur, warning that assigning a type number to a string is invalid.

Currently, there are many proposed features for Type Annotations. They try to make it as similar to TypeScript as possible, as this is the best way to support typing in JavaScript and remain compatible with TypeScript.

Some examples include:

Variable types:

let x: string;

Type:

type CoolBool = boolean;

Interface:

interface Person {
    name: string;
    age: number;
}

Export:

export interface Person {
    name: string;
    age: number;
}
export type { SomeLocalType };
export type { SomeType as AnotherType } from "some-module";

And many other definitions. For more details, readers can visit the description page of Type Annotations.

If TypeScript is already good, why introduce Type Annotations?

First, it must be said that TypeScript is not JavaScript, so it is not under the control of TC39.

Second, the most requested feature in JavaScript by the community is type support. According to the JavaScript survey in 2020 and 2021, it has always been at the top of the developers' wishlist.

JavaScript survey

However, TypeScript has done a great job with this type system - it has been widely used with many indications that people want to continue using it. If TC39 were to create something completely new, would it cause difficulties for the development community?

Therefore, they may want to find a solution to reconcile the community and type-checking languages based on JavaScript. Since JavaScript is already widely developed, it cannot simply change everything to become a typed language, nor does it want to "reinvent the wheel" on its own.

If that becomes a reality, TypeScript can now run directly in the browser without the need to "build" into JavaScript, significantly optimizing the developer experience.

At the same time, this is also the standard foundation for typing JavaScript. By standardizing data types, it will serve as a basis for all type compilers, making things easier for developers.

Conclusion

TC39 is responsible for developing the JavaScript programming language. New features must go through 5 stages to be implemented in practice. Recently, Type Annotations is an effort to bring data typing to JavaScript, but in a more "relaxed" way rather than being restrictive. What do you think about this issue? Personally, I think it is worth looking forward to. As there are still many changes before Type Annotations are officially introduced, I hope it will be good enough to reduce the complexity of type checking tools.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Trịnh Cường2 years ago
java is da bet@gif [MVDPX3gaKFPuo]
Reply