Skip to main content

Command Palette

Search for a command to run...

Writing and Executing Your First TypeScript Code

TypeScript 101 Series - Episode 1

Updated
โ€ข4 min read
Writing and Executing Your First TypeScript Code
S

Hi there! ๐Ÿ‘‹ I am a passionate and dedicated Web Developer with a keen interest in UI/UX design. I have a strong work ethic and am always eager to learn and grow in my field. I am also a technical blogger, sharing insights on web development and more.

If you're looking for a creative and driven developer who is always up for a challenge, I would love to connect with you! ๐Ÿ™Œ

Intro ๐Ÿ˜Ž

Welcome to Episode 1 of the TypeScript 101 Series! In this episode, Iโ€™ll give an overview of TypeScript, discuss its advantages over JavaScript, and show you how to set up a TypeScript project and execute your TypeScript code.

Why learn TypeScript when I know or do not know Javascript? ๐Ÿค”

โ€ข If you know JavaScript: TypeScript is a superset of JavaScript, which means that it can be used to write JavaScript code. However, TypeScript also adds features that can help you write more robust and maintainable code, such as static typing and type inference.

โ€ข If you don't know JavaScript: TypeScript is a good language to learn if you're new to programming. It's a relatively easy language to learn, and it can help you avoid some of the common pitfalls that new programmers make.

Donโ€™t believe me? Here are the benefits of using TypeScript โœ…

  • Static typing: Static typing helps to prevent errors by ensuring that the types of variables and expressions are known at compile time. This can help to catch errors early before they cause problems in your application.

  • Type inference: Type inference allows you to omit the types of variables and expressions in many cases, which can make your code more concise and easier to read.

  • Refactoring safety: TypeScript's refactoring safety features help to protect your code from errors when you make changes to it. This can save you a lot of time and frustration.

  • Large ecosystem: TypeScript has a large and active ecosystem, with many libraries and tools available to help you develop your applications.

Letโ€™s Get started! ๐Ÿฅณ

Before we get started I want you'll to keep this in mind:

TypeScript is just a development tool, your project still runs JavaScript!

Project Setup ๐Ÿ“ƒ

Letโ€™s create a new folder โ†’ ts-example

To get started with TypeScript, you'll need to install it on your machine. You can do this using a package manager like npm or yarn.

npm i -g typescript

Once you have TypeScript installed, you can create a new TypeScript project using the tsc --init command, which will generate a tsconfig.json file that configures the TypeScript compiler for your project. (donโ€™t worry about it now, weโ€™ll take a look at what it is later)

tsc means TypeScript Compiler

Wow! Now you are all ready to start practically learning TypeScript!

Writing your first TypeScript Code! ๐Ÿ˜‰

Create your first TypeScript file โ†’ index.ts

Open index.ts in your favorite code editor, and let's start writing some TypeScript code.

We'll create a simple function that greets the user.

function greet(name: string) {
  console.log(`Hello, ${name}!`);
}

greet("TypeScript");

Code Explanation

In the above code, the greet function takes in a parameter - name and since we are using TypeScript we can define what type of variable it is, here we are specifying it to be a string! ( Static Typing remember? ๐Ÿ˜Ž).

Compiling TypeScript to JavaScript ๐Ÿ‘จโ€๐Ÿ’ป

Let's compile our typescript code using tsc .

tsc index.ts
npx tsc index.ts // If the above command doesn't work.

This will generate the equivalent js file!

Running your code ๐Ÿƒโ€โ™‚๏ธ

Finally, you can run your compiled JavaScript code using Node.js. In the terminal, enter the following command

node index.js

You should see the output "Hello, TypeScript!" in the console.

Congratulations!๐Ÿฅณ You've successfully written and executed your first TypeScript code. (CHEERS!)

Summary ๐Ÿ“

  • We got introduced to TypeScript and wrote our first TypeScript code.

  • We covered how to set up a simple TypeScript project, write a simple TypeScript function, compile TypeScript to JavaScript, and run the compiled code.

  • Now that you have a basic understanding of how to write TypeScript code, you're ready to explore the language further and leverage its powerful features in your projects.

Stay tuned for the next blog post in our TypeScript 101 series, where we'll dive deeper into TypeScript's type system and explore how it can help catch errors at compile-time and improve the robustness of your code!

Till then Happy Coding! ๐Ÿ˜‰

A

I am facing an issue in writing the code

S

What is it?

TypeScript 101 Series

Part 1 of 3

TypeScript 10 Series: Beginner's guide to TypeScript. Learn static typing, interfaces, classes, and more. Level up your JavaScript skills. Write cleaner, maintainable code.

Up next

Understanding Primitive Types, Type Annotations and Non-Primitive Types in TypeScript

TypeScript 101 Series - Episode 2