DEV Community

Cover image for What is TypeScript?
Ghameerah McCullers
Ghameerah McCullers

Posted on

What is TypeScript?

An introduction to the strongly-typed language TypeScript and why you might want to learn it.

TypeScript: A JavaScript superset

When it comes to programming tools, Javascript is a beast of a language with a vast array of viciously competing libraries and frameworks. If you're like me and find yourself getting frustrated reading documentation and annoyed with all the changes the API makes in a blink of an eye, then you are not alone.

TypeScript was introduced as a JavaScript superset and was created released by Microsoft in 2012. TypeScript is JavaScript with syntax for types. What are types? Types give you the authority to describe your data. Types allow you to describe the shape of objects and functions in your code. Type checkers are type police built into JavaScript to enforce the type laws (for example, it's against the rules to multiply a string with an array). Type checking is the process where type checkers verify that your code follows all of the rules of the language.

JavaScript is a dynamically typed language, meaning, a variable declared as a number can be turned into a string. Comparatively, TypeScipt is a statically typed language, meaning you declare beforehand what type of value the variable will hold and it doesn't change. Think of TypeScript as your code's enforcer on rules. TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor.

The beauty of TypeScript is that it makes it possible to see documentation and issues directly in your editor, no more waiting until runtime to know something in your code broke. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. As your codebase continues to grow, so does your enforcement of rules.

In dynamically-typed languages like JavaScript, type checking occurs during runtime. TypeScript is so sweet that it will help developers identify errors before runtime. The browser can't actually execute typescript, so your .ts file or .tsx file is compiled to Javascript at runtime. TypeScript code converts to JavaScript, which runs anywhere JavaScript runs: In a browser, on Node.js or Deno, and in your apps. Statically-typed languages like TypeScript check types during compile time - right in your text editor!

Basic Type Definitions:

let id: number = 5
let company: string = 'Ghameerah'
let isPublished: boolean = true
let x: any = 'Hello'
let ids: number[] = [1,2,4,7,8]
Enter fullscreen mode Exit fullscreen mode

Summary of Benefits of TypeScript:

  • Similar to static languages such as Java or C#
  • Extra error checking
  • Runtime errors caught early during development
  • Avoid unwanted behavior at runtime
  • Mitigation Strategies
  • TypeScripts helps developers write better code
  • Make sure type conversion doesn't happen
  • Add static types
  • Explicitly naming types in your code
  • Explicitly assign types to variables
  • Helps with predictability

Other Resources

React TypeScript Cheatsheet
https://react-typescript-cheatsheet.netlify.app/

Top comments (0)