What is TypeScript?

Let's explain! πŸ§΅πŸ‘‡

#100DaysOfCode
#CodeNewbie
#DEVCommunity
#typescript
πŸ”Œ As always, this thread is also available in a more accessible and more permanent space together with my other threads here πŸ‘‡

🌐 letsexplain.netlify.app/typescript
Take a look at this Javascript code:
What is the output of the code?

If your answer is 15 then you're right! You have two numbers, "number1" and "number2". If you add them together using the "+" sign, it will give you the total because they're both numbers!
Now take a look at this:
This time "number1" and "number2" are strings. So what will this code give as output?

If your answer is "510" but as a string, then you're right again!
But that's not how you expect it to work.

I want "number1", "number2", and "sum" to all be numbers so that "sum" gives me the sum every time.
Wouldn't it be nice if we could put some rules in so that we're absolutely CERTAIN that `number1`, `number2` and `sum` are ALWAYS numbers?
Let's look at another example:

Suppose, this time, we want to divide two numbers together:
The result of the above is (as you'd expect), 5. But what if instead of a number, `number2` is a string, or an array?
What would you expect it to happen here?

If your answer is "it would give me an error" then you'd be wrong. The result would be "Infinity". But that is non-sense!

There should be some rules in place that say "you can only divide numbers with numbers". Wouldn't that be nice?
Enter TypeScript!

TypeScript is a language that is basically extended Javascript.

In essence, TypeScript allows you to write Javascript so that when you declare variables or functions, you can also specify what TYPE each of them are (string, number, array etc).
For example:

"I want to create a variable total which is a number"
"I want to create a variable `name` which is a string":
"I want to create a function `getTotal` which should always return a number":
So what would happen if I write a string in the total variable in my TypeScript (.ts) file, since it expects a number? This:
This is what's so amazing about TypeScript. It tells you that what you're assigning is wrong before you even hit save! That means this error never makes it to your users, and it makes fixing errors a lot LOT easier!

So how do I get started?
You can install TypeScript using via NPM:

npm install -g typescript
You can then turn a TypeScript (.ts) file into a Javascript (.js) file by running this command:

npx tsc myfile.ts

You can then attach your JS file to your HTML as normal!
This thread only touches the absolute basics of TypeScript but you can learn a lot more on the TypeScript official documentation which is quite comprehensive!

typescriptlang.org/docs

Enjoy!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Savvas Stephanides

Savvas Stephanides Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SavvasStephnds

12 Nov
βš› What is React?

Let's explain! πŸ‘‡πŸ‘‡πŸ‘‡

#100DaysOfCode
#CodeNewbies
#DEVCommunity
#reactjs
React is a framework that makes it easier to create complex web pages. How does it do that?
Imagine you're developing a to-do app. Let's say you just need to display 4-5 tasks. Ignore functionality like adding, or marking tasks as complete.
Read 25 tweets
15 Oct
What is Docker? Let's explain! πŸ‹πŸ³

(Thread) πŸ§΅πŸ‘‡

#100DaysOfCode
#CodeNewbies
#docker
πŸ”ŒBefore I begin: This thread is also available in a more convenient and more accessible format at my new project "Let's Explain" (where you'll find all my other "let's explain" threads as well):

β–Ά letsexplain.netlify.app/docker
So you've just built a project. Let's say it's a to-do app. There are plenty of things involved:

- front-end
- back-end
- database...
Read 20 tweets
1 Oct
What is async/await in Javascript? Let's explain

(Thread) πŸ‘‡πŸ§΅

#100DaysOfCode
#CodeNewbies
We've already talked about Promises in a previous thread. Read it if you haven't already. This thread will be a continuation of that thread so please see that first:

So as we mentioned before, Promises are a way of telling Javascript, "go fetch some data but I'm not going to sit and wait because my browser would freeze. When you DO get a response eventually, do THIS with the data".
Read 20 tweets
26 Sep
β™ΏοΈπŸ¦Ύ ACCESSIBILITY QUIZ!

10 questions. Have fun!

#100DaysOfCode #a11y #CodeNewbie

🧡 πŸ‘‡
QUESTION 1:

The standards for web accessibility are called:

A. Accessible Web Initiative
B. Web Accessibility Working Group
C. Web Content Accessibility Guidelines
ANSWER: C
Read 22 tweets
22 Sep
❌ β€œI built a to-do app with React”

βœ… β€œI have built an app that has helped people stay on top of their day-to-day tasks and stay organised using my React skills”

πŸ‘‰ It’s about the impact you have on people, not your skill set
❌"I built a weather app with Vue"

βœ… "Enabled users to plan their day by building an app which provides clear and convenient hourly weather forecasts. Make flexible, expandable and fast by using the Vue framework to build the app"
❌ "Created a website for a restaurant"

βœ… "Enabled a local restaurant to quickly and conveniently communicate with their customers about their menu options and latest offers by creating a fast website for their establishment, thus increasing their profits by 15%"
Read 4 tweets
21 Sep
What is "this" in Javascript? Let's explain:

Thread πŸ§΅πŸ‘‡

#100DaysOfCode #CodeNewbies
First off, a note: I was planning on create a thread like this for ages, but I have been putting it off for one simple reason: just like a lot of people, I too didn't quite get the "this" keyword. It still baffles me a bit but hopefully this basic walkthrough will make sense.
"this" is basically a label. It's a label you put on a function to show who it belongs to. Sort of like putting a label on your food at work saying "THIS SANDWICH BELONGS TO <your name>"
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!