Guess what, JavaScript is getting types!?

Guess what, JavaScript is getting types!?

Daily short news for you
  • I just discovered the idb-keyval library that helps implement a key-value database simply. As shared in the series of posts about the process of making OpenNotas, I was struggling to find a type of database for storage, and it seemed quite difficult; in the end, I settled on localForage.

    idb-keyval is quite similar to localForage, but it seems to be doing a little better. For example, it has an update function to update data, simply imagine it like this:

    update('counter', (val) => (val || 0) + 1);

    Unlike the set function, which completely replaces the data.

    » Read more
  • At the beginning of the new year, may I share the amount earned after 1 month of advertising at indieboosting.com 🥳🥳🥳

    » Read more
  • Since the Lunar New Year holiday has started, I won't be posting anymore. See you all after the holiday! 😁

    » 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

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? 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ường1 year ago
java is da bet@gif [MVDPX3gaKFPuo]
Reply
Scroll or click to go to the next page