Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Typescript

100 Days of TypeScript (Day 1)

Rate me:
Please Sign up or sign in to vote.
4.26/5 (9 votes)
25 Oct 2021CPOL4 min read 10.7K   17   13
Getting started with TypeScript
This is Part 1 of a 100 part series introducing you to TypeScript if you’ve not used it before; which will give you a brush up on things you already know if you’ve used it, and maybe introduce you to some new things that you might not have come across before. In this post, you will learn how to get started with TypeScript.

Introducing TypeScript

It’s been a while since I’ve had the chance to write in this blog and I really wanted to come up with something different for me. If you’re a long time follower of mine, you probably know that I wrote a book on TypeScript a couple of years ago, so I thought it would be fun for me to embark on one of those 100 Days of types of blog and write a series introducing you to TypeScript if you’ve not used it before; give you a brush up on things you already know if you’ve used it, and maybe introduce you to some new things that you might not have come across before.

Note: This blog series isn’t going to be a continuous 100 days of posting, it’s 100 days by the end.

So, what is TypeScript? If you go to the TypeScript website, the tagline is “TypeScript is JavaScript with syntax for types”. What does that actually mean?

TypeScript was originally built out of the idea that writing high quality JavaScript was difficult. While that’s a subjective opinion, TypeScript rapidly evolved, giving us the ability to use up and coming JavaScript features via polyfills (don’t worry about this, we’ll come back to this in a future post). TypeScript now ranks as one of the most popular languages around.

Getting Started

Okay, you can develop TypeScript apps online in the playground, but I really recommend installing it locally. To do that, you’re going to need to use something like npm, yarn or pnmpm. If you don’t have npm installed, you need to install Node to get it. Assuming you have installed Node, you can install TypeScript with the following command (depends on the package manager you’re using).

Shell
npm install -g typescript

It’s time to write our first TypeScript application (all code is available in github).

This is going to be a simple program to add two numbers together. I’m going to use the TypeScript compiler to set some things up ready for when I want to compile my TypeScript code.

Shell
tsc --init

This has created a file called tsconfig.json which sets up compiler options that determine what our JavaScript will look like when we’ve compiled it from TypeScript; oh wait, didn’t I mention that TypeScript compiles down to JavaScript? A quick note – I pretty much always use Visual Studio Code for editing my TypeScript; it’s a great choice if you’ve not used it before.

I’m going to add a file called day1.ts to add my TypeScript to add my first piece example. The .ts extension tells us that this is a TypeScript file. If you have used JavaScript before, the code will look familiar to you. This is what our add function would look like as a JavaScript method.

TypeScript
function add(number1, number2) {
    return number1 + number2;
}

I said that I wanted the add function to be able to add two numbers. I don’t want it to add two strings together or a date and a string. This is where the first strength of TypeScript really comes into its own, and where the TypeScript tagline makes sense. I am going to constrain the function to accept numbers and return a number using the following syntax.

TypeScript
function add(number1: number, number2: number): number {
    return number1 + number2;
}

If I attempt to pass something that’s not a number to either of the parameters, I won’t be able to compile the code. I can’t pass “special” values such as undefined or null to either of the parameters. In other words, I have just written something that will protect me from myself.

In order to test my code, I’m going to call the function and pass the output to the console window like this.

TypeScript
console.log(add(10, 20));

After saving the file, I want to “compile” the TypeScript code so that it turns into JavaScript. To do this, I simply run the tsc command with no parameters. This picks up the contents of the tsconfig.json file and uses that to control how the file is compiled. (The tsc command here should be run in the same directory as the tsconfig.json file).

Now that I have compiled my code, I can run it using the following command.

Shell
node day1.js

And that’s it; that’s the first TypeScript program. On day 2, we are going to look at how to change our function over so that it sits inside a class; which will let us see how we can start to leverage object oriented concepts to build reusable blocks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

Comments and Discussions

 
GeneralMy vote of 5 Pin
raddevus27-Oct-21 11:22
mvaraddevus27-Oct-21 11:22 
GeneralRe: My vote of 5 Pin
Pete O'Hanlon28-Oct-21 0:54
mvePete O'Hanlon28-Oct-21 0:54 
QuestionSo 99 more days to come Pete? Pin
Sacha Barber26-Oct-21 2:08
Sacha Barber26-Oct-21 2:08 
AnswerRe: So 99 more days to come Pete? Pin
Pete O'Hanlon26-Oct-21 3:15
mvePete O'Hanlon26-Oct-21 3:15 
GeneralRe: So 99 more days to come Pete? Pin
Sacha Barber29-Oct-21 10:44
Sacha Barber29-Oct-21 10:44 
GeneralRe: So 99 more days to come Pete? Pin
Pete O'Hanlon30-Oct-21 0:27
mvePete O'Hanlon30-Oct-21 0:27 
GeneralRe: So 99 more days to come Pete? Pin
Sacha Barber1-Nov-21 4:17
Sacha Barber1-Nov-21 4:17 
GeneralRe: So 99 more days to come Pete? Pin
Pete O'Hanlon1-Nov-21 4:26
mvePete O'Hanlon1-Nov-21 4:26 
GeneralRe: So 99 more days to come Pete? Pin
Sacha Barber29-Oct-21 10:45
Sacha Barber29-Oct-21 10:45 
SuggestionHeaders Pin
Richard Deeming25-Oct-21 5:21
mveRichard Deeming25-Oct-21 5:21 
GeneralRe: Headers Pin
Chris Maunder25-Oct-21 7:00
cofounderChris Maunder25-Oct-21 7:00 
GeneralRe: Headers Pin
Pete O'Hanlon26-Oct-21 1:10
mvePete O'Hanlon26-Oct-21 1:10 
GeneralRe: Headers Pin
Pete O'Hanlon26-Oct-21 1:09
mvePete O'Hanlon26-Oct-21 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.