DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • JavaScript for Beginners: Assigning Dynamic Classes With ngClass
  • TypeScript: Useful Features
  • Automating the Migration From JS to TS for the ZK Framework

Trending

  • A Modern Stack for Building Scalable Systems
  • Debugging With Confidence in the Age of Observability-First Systems
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Spring and PersistenceContextType.EXTENDED
  1. DZone
  2. Coding
  3. JavaScript
  4. Types May Finally Be Coming to JavaScript

Types May Finally Be Coming to JavaScript

Let's look at how types might work when they finally come to Javascript

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Apr. 18, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

With the promotion of Type Annotations to Proposal Level 1 Stage, JavaScript is one step closer to being a more strongly typed language. Let's dive into what the changes will mean for JavaScript.

Types in JavaScript

JavaScript is dynamically typed. That means that it figures out the type of something by where it appears. For example:

JavaScript
 
let x = 1; // typeof "number"
let y = "1"; // typeof "string"


In complicated application and systems, this is a huge weakness. Applications can fall over for the smallest of things - such as your API returning "false" instead of false.

That means we have to sometimes bulletproof our code by doing silly things like this, especially in instances where we don't have control over a piece of software sending back wonky types:

JavaScript
 
if(x === true || x === "true") {
    // this is true
}


This is a huge annoyance for dyed in the wool software engineers, but for beginners, JavaScript is a gift. JavaScript makes it so easy to write quite complicated things in a simple way. If your code is wrong, JavaScript usually finds a way to make it work. Most JavaScript developers find that the flexibility they love at the start, soon becomes painful as they become more competent in the language.

For these reasons, there has never been a strong push to make JavaScript a typed language, with hard rules. The accessibility of JavaScript is one of its great traits, and adding types would not only make it harder for beginners, but also potentially break a lot of the existing web technologies built on untyped JavaScript.

As such, optional packages were developed instead - TypeScript and Flow being two of the best known. These take JavaScript and turn it into a language with strongly declared types.

Enter: JavaScript Types

The new type annotations proposal tries to find a middle ground, so JavaScript can remain accessible, but allow for advanced features like types more easily.

In some ways, this was an inevitability. Strong types consistently come out on top of the most requested developer features for JavaScript:

Most requested Javascript features

How Type Annotations Will Work

To be clear, type annotations will not make JavaScript a strongly typed language. Instead, they let JavaScript work with types. What that means is, if you add types to JavaScript once this specification reaches an implementation stage, they will simply be ignored. Should you compile it with Typescript, though, the types will work as normal.

If you've used TypeScript before, then, you'll be familiar with how type annotations look. For example, defining a type for a variable looks like this:

JavaScript
 
let x:string = "String";
let x:number = 5;


How JavaScript Would Work After Type Annotations

Let's say you write a piece of code that looks like this:

JavaScript
 
let x = "String";
console.log(x);


Later down the line, you decide you want to add types. Instead of having to compile it with TypeScript, with type annotations we can simply add the types to our JavaScript file:

JavaScript
 
let x:string = "String";
console.log(x);


In this case, :string is simply ignored by JavaScript - meaning it remains compatible with untyped JavaScript, and type bundles like TypeScript. That means you can build it with TypeScript, but you can also just run it as a normal .js file. Types in a vanilla JavaScript file are ultimately comments.

Other Examples of Types in Type Annotations

The general roster of types that you'd expect can be found in type annotations. For example -

Interfaces

JavaScript
 
interface Name {
    firstName: string,
    lastName: string
}


Union Types

JavaScript
 
function test(x: string | number) {
    console.log(x);
}


Generic Types

JavaScript
 
type Test<T> = T[];
let x:Test<number> = [ 1, 2, 3, 4 ];


Rationale Behind Type Annotations

The main rationale is that as browser development has become more streamline, and backend JavaScript ecosystems like Deno and Node.JS have become more popular, there have been opportunities for JavaScript to move to less and less build steps. The big change comes with TypeScript, where to run a TypeScript file, we need to build it back to JavaScript. This is obviously a time consuming and sometimes onerous step for developers, and definitely limits efficiency.

One of the main draws to type annotations in TypeScript is that it removes another build step. Native TypeScript can be run in the browser without the need to convert it back to JavaScript, greatly optimising the developer experience.

As well, this type annotation acts give a standard base to type definitions in JavaScript. By standardising the most agreed upon parts of types in JavaScript, it ensures consistency across all type compilers, making things easier for developers.

Conclusion

In conclusion, types are coming to JavaScript, but perhaps not in the way people envisioned. JavaScript will remain a dynamically typed language, but it will accept type declarations natively. This specification is still stage 1, so a lot may change - but it's an exciting time for JavaScript to take its first step into a more strongly typed world.

JavaScript TypeScript

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • JavaScript for Beginners: Assigning Dynamic Classes With ngClass
  • TypeScript: Useful Features
  • Automating the Migration From JS to TS for the ZK Framework

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!