Getting Started with TypeScript

in #utopian-io7 years ago (edited)

We had been using JavaScript for decades, why do we want to learn a new language? For me, TypeScript makes me feel faster in development because I do not need to care about the browser support and testing on each browser because you can set up the browser target that you wan with typescript. Although this can be achieved with Babel, but TypeScript brings in a lot of new modern features in JS.

Therefore, in this post, I am talking about what is typescript, why typescript and what else typescript offers.


bynNY5dJ.jpg

Source

Starting with Typescript

Typescript is language develop by Microsoft. It is a superset of Javascript, meaning that it has more features than Javascript but it still works on all browser.

Typescript will be compiled to Javascript

What is Typescript?

  • A typed superset of JS
  • Compiles back to JS which is compatible with both Web or Node
  • Made up from 3 parts:
    • Language - The language itself
    • Language Service - Helps in developer experience
    • Compiler - Compile to JS (Back to ES3!)
  • Seems to be leading the way JS language features

Javascript Types

  • null
  • undefined
  • boolean
  • number
  • string
  • symbol (ES6)

Everything else is extends from Object

Read more on my previous post: JS let, const, var

const, let, var

Read more on my previous post: JS let, const, var

Why add Types?

Since JS is a dynamic language, why do we need types in js?

  • JS sometimes operate without explicit instruction to convert primitive value
  • prevent run time error (the error move to compile time)
  • Suitable for other developer to understand
  • Object Oriented concept built in
  • Increase your code efficiency because the types system will optimize by the browser.

Example

let length = 2;
length = '3';

Typescript will yell at us: Type 'string' is not assignable to type 'number'.

Therefore, after a variable is assign a type, we should not change it's type.

Get started

Typescript is a Structural Type System, which means TS only cares about the shape of the object instead of checking the whether the variable is an instance of a class or type named.

Object Shape

Imagine a house, there is windows, doors and your house address. Since we know that all these properties had specific data types, we can implement this into TS.


home.png

So, to create a house object with typescript, we state how the house object shape.

let myHouse: {windows: number, doors: number, address: string};
myHouse = {
    windows: 10,
    doors: 3,
    address: "17, Jalan Tempat"
}

If the object has excess properties, it should be okay as long as it fits the shape that you declared previously.

let myHouse: {windows: number, doors: number, toilets: number, address: string};
myHouse = {
    windows: 10,
    doors: 3,
    address: "17, Jalan Tempat"
}

To get started

Go to typescript playground

This playground can let you get started with TypeScript quickly. In the left is the code in TypeScript, and on the right is the compiled JavaScript
Screen Shot 2017-12-30 at 10.17.56 PM.png

Alternatively, use Code Runner Plugin in Visual Studio Code

Screen Shot 2017-12-30 at 10.40.57 PM.png

This plugin required 2 global npm package which is ts-node and typescript.

to install:

  • npm install -g ts-node or yarn global add ts-node
  • npm install -g typescript or yarn global add typescript
    if you are not familiar with npm or yarn, check out my tutorial

Press the play button on VS Code to test out your code without any tsc workflow setup.

play.png

This post basically provides you enough to get started with TypeScript by testing all the APIs of TypeScript without setting up all those complicated workflow. Later on, I will create topics on how to setup your workflow with TypeScript.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

This post has received a 0.15 % upvote from @drotto thanks to: @banjo.

Congratulations @superoo7! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

thank you moderator

The @OriginalWorks bot has determined this post by @superoo7 to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Hey @superoo7 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63235.53
ETH 2558.76
USDT 1.00
SBD 2.63