Lädt...

🔧 TypeScript Chronicles: Navigating the World of Typed JavaScript🚀


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In the dynamic landscape of web development, TypeScript is a trusty ally for developers. It adds some helpful features to JavaScript, making coding easier and less error-prone.

In this post, we'll take a closer look at Typescript and how it can make your coding life better.

Let's dive in!

Setting up TypeScript

Step 1 - Install TypeScript globally

npm install -g typescript

Step 2 - Initialize an empty node.js project with tsc

mkdir ts-app
cd ts-app
npm init -y
npx tsc --init

*All the above commands will initialize package.json and tsconfig.json files in your project

Step 3 - Now create a index.ts file in ts-app directory

tsc hello world code

Step 4 - Compile the ts file

tsc -b

it will generate a new index.js file

js file

Notice, this is plain JavaScript code with no types

TypeScript vs. JavaScript: Bridging the Gap

TypeScript is a superset of JavaScript. It allows developers to declare types explicitly making it easy to catch error at compile time

While JavaScript is a powerful flexible language but loosely typed which can lead to unexpected errors, especially in larger projects.

let's try assigning x to a number:

let x: string = "Hello world!";
x = 1
console.log(x);

Try to compile the code again. And it will throw an error

error

This is the benefit of TypeScript. It adds a layer of safety by allowing you to specify the types of data your code expects.

To know more about the types you can go through the official docs of TypeScript ts docs

tsconfig file

tsconfig file has many options available. You can also change those to change the process of compilation

If you want to learn about the tsconfig file and its usability in compilation process in depth you can go through this article 👉 tsconfig-indepth

Interfaces

you can assign types to objects using interfaces

interface example

Types

type can be used to combine existing types or create complex types.
with type you can create union types and intersection types easily

example of type

Type vs Interface

If you want to create a new data type that represents complex types or a combination of existing types, 'type' may be more appropriate. If you're defining the structure of an object, interface is likely the better choice.

Sometimes it may come down to personal preference. Both type and interface are powerful features of TypeScript, so choose the one that best fits your needs and makes your code easier to understand.

Enums

You can define a set of constants using Enums

Generics

Generics in TypeScript also provides a way to create reusable components that can work with different data types.

They are very much useful in libraries and frameworks where the input and output data types may vary

Here are some useful resources for learning TypeScript

ts-docs
ts-cheatsheet
ts-tutorial
ts-beginners-guide

Happy Coding!⭐

...

🔧 TypeScript Chronicles: Navigating the World of Typed JavaScript🚀


📈 64.52 Punkte
🔧 Programmierung

🔧 Is JavaScript Statically Typed or Dynamically Typed? 🤔


📈 43.38 Punkte
🔧 Programmierung

🔧 Why TypeScript? A Beginner’s Guide to Typed JavaScript


📈 33.18 Punkte
🔧 Programmierung

🔧 TypeScript for Beginners: Your First Steps into Typed JavaScript


📈 33.18 Punkte
🔧 Programmierung

🔧 🚀 TypeScript: A Strongly Typed Superset of JavaScript


📈 33.18 Punkte
🔧 Programmierung

🔧 The Rise of TypeScript: A Strongly Typed JavaScript Superset


📈 33.18 Punkte
🔧 Programmierung

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 29.8 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 29.8 Punkte
🕵️ Reverse Engineering

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 29.8 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 29.8 Punkte
🔧 Programmierung

🔧 Why you should learn TypeScript and ditch JavaScript? TypeScript vs JavaScript


📈 28.52 Punkte
🔧 Programmierung

🔧 TypeScript ✔ vs JavaScript ❌ : How TypeScript Outshines JavaScript


📈 28.52 Punkte
🔧 Programmierung

🔧 TypeScript ✔ vs JavaScript ❌ : How TypeScript Outshines JavaScript


📈 28.52 Punkte
🔧 Programmierung

🔧 TypeScript ✔ vs JavaScript ❌ : How TypeScript Outshines JavaScript


📈 28.52 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed


📈 27.65 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed - Intro: reliability and productivity


📈 27.65 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed - Part 1: configuring a project


📈 27.65 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed - Part 2: full coverage typing


📈 27.65 Punkte
🔧 Programmierung

🔧 Typed Objects in v-for - Vue3 with TypeScript


📈 27.65 Punkte
🔧 Programmierung

🔧 Building Strongly Typed REST Clients with TypeScript


📈 27.65 Punkte
🔧 Programmierung

🔧 How to Create Strongly-Typed Schemas in TypeScript for Ethereum Events


📈 27.65 Punkte
🔧 Programmierung

🔧 My useful library for typed RPC calls in TypeScript


📈 27.65 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed - Part 4: full static types


📈 27.65 Punkte
🔧 Programmierung

🔧 TypeScript strictly typed - Part 3: safe nullability


📈 27.65 Punkte
🔧 Programmierung

🔧 ZooKeeper Chronicles: Navigating EOFException and the Enigma of 0-Length Files


📈 26.03 Punkte
🔧 Programmierung

🔧 Why use javaScript typed arrays


📈 24.46 Punkte
🔧 Programmierung

🔧 Typed JavaScript? For real? The type annotations proposal and what it’s all about


📈 24.46 Punkte
🔧 Programmierung

🔧 Typescript Coding Chronicles: Can Place Flowers


📈 24.27 Punkte
🔧 Programmierung

🔧 Typescript Coding Chronicles: Reverse Vowels of a String


📈 24.27 Punkte
🔧 Programmierung