SlideShare a Scribd company logo
1 of 7
Download to read offline
An Introduction to TypeScript
The front-end React developer world is all abuzz with the fondness of using and
preferring TypeScript over JavaScript. Although it’s not recommended for all
types of projects it strongly overcomes many shortcomings of JavaScript and
improves over it.
In this beginner-friendly article, we will get to know what exactly TypeScript is,
how is it a strongly-typed language, how it compares to JavaScript along with
some of its highlighting features. Of course, we will be writing our first .ts code
too!
What is TypeScript?
TypeScript is a strongly typed programming language that builds on JavaScript
giving you better tooling at any scale. It’s a free and open-sourced project
created by Microsoft.
It is a ‘superset of JavaScript’, which means that you can continue to use the
JavaScript skills you’ve already developed and add certain features that were
previously unavailable to you. When compared to JavaScript, it’s a strongly
typed language as opposed to JS which is a loosely typed language. You can
consider this like JavaScript with superpowers!
Now here’s where this language actually shines…remember the term
‘strongly typed’ we used above? What does it mean in this context? Well, this
means that the data types of variables/functions and other primitives must
be pre-defined. This is one of the most important features of TypeScript (that’s
why it focuses so much on the ‘type’).
Under the hood, it compiles to JavaScript, giving you the benefit of the
JavaScript platform plus the intended advantages of types.
Top features of TypeScript
Now that you know a bit about this language, it’s time to see all the important
and useful features it provides to the developer. Here are a few of them:
1. JavaScript and more: TypeScript adds additional syntactic sugar to your
JavaScript code to support a tighter integration with your editor.
2. Runs anywhere where JavaScript does: TypeScript code converts to
JavaScript which can then be run in a browser, on Node.js or Deno, and in your
apps.
3. Safety with scalability: it uses type inference to give you great tooling without
writing any additional code.
4. Editor support: most of the modern IDEs and code editors like VS Code come
with built-in support for TypeScript files. You get autocompletion and auto-
import support in VS Code out of the box.
5. Unique language features: here are some of the features which you will only
find in a TypeScript code; Interfaces, Namespaces, Generics, Abstract classes,
Data modifiers, and more!
6. Gradual adoption rate: you can apply the types to any previous JavaScript
projects or codebase incrementally. With great editor support, TypeScript
catches errors right inside your editor!
7. Easy to describe the data: it’s really easy to describe the shape of objects
and functions in your code. This makes it possible to see documentation and
issues in your editor.
All of this should give you a general idea of what TypeScript is and what are its
features, it’s time to write our first TypeScript code and see how to use it with
JavaScript gradually.
From JavaScript to TypeScript
We won’t be diving straight into a TypeScript code. Instead, we want you to
get familiar with an already existing knowledge of JavaScript and use it to
convert a small JS code to TS code.
Let’s say we have the following JavaScript code:
// @ts-check
function compact (arr) {
if (orr. length > 10)
return arr. trim(0, 10)
return arr
}
Now you will see an error like “Cannot find name ‘orr‘.” Next, let’s say we
do another mistake like using
trim instead of slice on an array:
function compact (arr: string[]) {
if (arr.length > 10)
return arr.slice(0, 10)
return arr
}
We add a type of string[] (String array) for the arr parameter so it should always
accept a string-based array and nothing else. Hence, we call TypeScript
‘strongly-typed’.
Install and Setup TypeScript
Time to write some TS code locally on our machine! You can install TypeScript
globally via the following NPM command:
npm install -g typescript
Next, you can confirm the installation by running tsc –v to check the version of
TypeScript installed in your system.
Note that after you write a TypeScript code and want to run it, simply
running tsc with file, the name won’t work as tsc is just a TypeScript compiler.
We need Node.js to get the actual log output. We can do it by running this
command for a “Hello World” program:
tsc hello.ts && node hello.js
Your first“Hello World!” in TypeScript
After you installed TypeScript globally on your machine. You can open a suitable
code editor like VS Code which has excellent support for the TypeScript tooling.
1. Create a new TypeScript file called helloWorld.ts. Then simply write a console log
statement as you would do in JavaScript:
console.log("Hello World!");
2. Open your command prompt or Terminal window and run tsc helloWorld.ts. You
will see nothing will happen as there are no types assigned here hence no type
errors.
3. Instead you will see the TypeScript compiler generates a new helloWorld.js file
in the same directory. This is the same TS code but it is the generated JS file
output.
4. Time to make our console statement better. Let’s say we want to log the
person’s name and date by asking the user to enter it through a greeter
function:
function greet(person, date) {
console.log(`Hello ${person}, today is ${date}!`);
}
greet('Brendan');
Notice the way we are calling the greet function. If you run this you will get this
error because we passed only 1 argument instead of the expected 2:
// TS ERROR: Expected 2 arguments, but got 1.
The parameters to the greet() function don’t have any explicitly defined types
so TS will give it any type.
5. Let’s fix our function with the following valid code:
// "greet() takes a person of type string, and a date of type Date"
function greet(person: string, date: Date) {
console.log(`Hello ${person}, today is ${date.toDateString()}`);
}
greet('Maddison', new Date());
Now we have explicitly defined all the data types and we can happily see the log
statement printing the exact output we need.
Just in case you are wondering the equivalent JS code of this will be:
// "greet() takes a person of type string, and a date of type Date"
function greet(person, date) {
console.log("Hello " + person + ", today is " + date.toDateString() + "!");
}
greet('Maddison', new Date());
With that, we have covered the bare-minimum basics you need to know of the
TypeScript language. As you saw, it’s very much close to JavaScript so if you
were already working with JavaScript then it should be easy to learn and migrate
your projects to TypeScript. To make your work easy, we’ve created
some dashboard templates. Check out now!
Source: https://blog.wrappixel.com/introduction-to-typescript/
**********

More Related Content

Similar to An Introduction to TypeScript

Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2Knoldus Inc.
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersRutenis Turcinas
 
Moving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowMoving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowFibonalabs
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxMalla Reddy University
 
Introjs10.5.17SD
Introjs10.5.17SDIntrojs10.5.17SD
Introjs10.5.17SDThinkful
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java scriptmichaelaaron25322
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponLaurent Duveau
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponLaurent Duveau
 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TSGrigory Petrov
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code維佋 唐
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?Surendra kumar
 
Typescript for the programmers who like javascript
Typescript for the programmers who like javascriptTypescript for the programmers who like javascript
Typescript for the programmers who like javascriptAndrei Sebastian Cîmpean
 

Similar to An Introduction to TypeScript (20)

Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
Type script
Type scriptType script
Type script
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
Type script
Type scriptType script
Type script
 
Moving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowMoving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should Know
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
Introjs10.5.17SD
Introjs10.5.17SDIntrojs10.5.17SD
Introjs10.5.17SD
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
 
Lecture-15.pptx
Lecture-15.pptxLecture-15.pptx
Lecture-15.pptx
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Csharp_Chap01
Csharp_Chap01Csharp_Chap01
Csharp_Chap01
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TS
 
CSC PPT 12.pptx
CSC PPT 12.pptxCSC PPT 12.pptx
CSC PPT 12.pptx
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
C# 6.0
C# 6.0C# 6.0
C# 6.0
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 
js.pptx
js.pptxjs.pptx
js.pptx
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?
 
Typescript for the programmers who like javascript
Typescript for the programmers who like javascriptTypescript for the programmers who like javascript
Typescript for the programmers who like javascript
 

Recently uploaded

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 

An Introduction to TypeScript

  • 1. An Introduction to TypeScript The front-end React developer world is all abuzz with the fondness of using and preferring TypeScript over JavaScript. Although it’s not recommended for all types of projects it strongly overcomes many shortcomings of JavaScript and improves over it. In this beginner-friendly article, we will get to know what exactly TypeScript is, how is it a strongly-typed language, how it compares to JavaScript along with some of its highlighting features. Of course, we will be writing our first .ts code too!
  • 2. What is TypeScript? TypeScript is a strongly typed programming language that builds on JavaScript giving you better tooling at any scale. It’s a free and open-sourced project created by Microsoft. It is a ‘superset of JavaScript’, which means that you can continue to use the JavaScript skills you’ve already developed and add certain features that were previously unavailable to you. When compared to JavaScript, it’s a strongly typed language as opposed to JS which is a loosely typed language. You can consider this like JavaScript with superpowers! Now here’s where this language actually shines…remember the term ‘strongly typed’ we used above? What does it mean in this context? Well, this means that the data types of variables/functions and other primitives must be pre-defined. This is one of the most important features of TypeScript (that’s why it focuses so much on the ‘type’). Under the hood, it compiles to JavaScript, giving you the benefit of the JavaScript platform plus the intended advantages of types. Top features of TypeScript Now that you know a bit about this language, it’s time to see all the important and useful features it provides to the developer. Here are a few of them:
  • 3. 1. JavaScript and more: TypeScript adds additional syntactic sugar to your JavaScript code to support a tighter integration with your editor. 2. Runs anywhere where JavaScript does: TypeScript code converts to JavaScript which can then be run in a browser, on Node.js or Deno, and in your apps. 3. Safety with scalability: it uses type inference to give you great tooling without writing any additional code. 4. Editor support: most of the modern IDEs and code editors like VS Code come with built-in support for TypeScript files. You get autocompletion and auto- import support in VS Code out of the box. 5. Unique language features: here are some of the features which you will only find in a TypeScript code; Interfaces, Namespaces, Generics, Abstract classes, Data modifiers, and more! 6. Gradual adoption rate: you can apply the types to any previous JavaScript projects or codebase incrementally. With great editor support, TypeScript catches errors right inside your editor! 7. Easy to describe the data: it’s really easy to describe the shape of objects
  • 4. and functions in your code. This makes it possible to see documentation and issues in your editor. All of this should give you a general idea of what TypeScript is and what are its features, it’s time to write our first TypeScript code and see how to use it with JavaScript gradually. From JavaScript to TypeScript We won’t be diving straight into a TypeScript code. Instead, we want you to get familiar with an already existing knowledge of JavaScript and use it to convert a small JS code to TS code. Let’s say we have the following JavaScript code: // @ts-check function compact (arr) { if (orr. length > 10) return arr. trim(0, 10) return arr } Now you will see an error like “Cannot find name ‘orr‘.” Next, let’s say we do another mistake like using trim instead of slice on an array: function compact (arr: string[]) { if (arr.length > 10) return arr.slice(0, 10) return arr }
  • 5. We add a type of string[] (String array) for the arr parameter so it should always accept a string-based array and nothing else. Hence, we call TypeScript ‘strongly-typed’. Install and Setup TypeScript Time to write some TS code locally on our machine! You can install TypeScript globally via the following NPM command: npm install -g typescript Next, you can confirm the installation by running tsc –v to check the version of TypeScript installed in your system. Note that after you write a TypeScript code and want to run it, simply running tsc with file, the name won’t work as tsc is just a TypeScript compiler. We need Node.js to get the actual log output. We can do it by running this command for a “Hello World” program: tsc hello.ts && node hello.js Your first“Hello World!” in TypeScript After you installed TypeScript globally on your machine. You can open a suitable code editor like VS Code which has excellent support for the TypeScript tooling. 1. Create a new TypeScript file called helloWorld.ts. Then simply write a console log statement as you would do in JavaScript: console.log("Hello World!"); 2. Open your command prompt or Terminal window and run tsc helloWorld.ts. You will see nothing will happen as there are no types assigned here hence no type errors.
  • 6. 3. Instead you will see the TypeScript compiler generates a new helloWorld.js file in the same directory. This is the same TS code but it is the generated JS file output. 4. Time to make our console statement better. Let’s say we want to log the person’s name and date by asking the user to enter it through a greeter function: function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } greet('Brendan'); Notice the way we are calling the greet function. If you run this you will get this error because we passed only 1 argument instead of the expected 2: // TS ERROR: Expected 2 arguments, but got 1. The parameters to the greet() function don’t have any explicitly defined types so TS will give it any type. 5. Let’s fix our function with the following valid code: // "greet() takes a person of type string, and a date of type Date" function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}`); } greet('Maddison', new Date()); Now we have explicitly defined all the data types and we can happily see the log statement printing the exact output we need. Just in case you are wondering the equivalent JS code of this will be:
  • 7. // "greet() takes a person of type string, and a date of type Date" function greet(person, date) { console.log("Hello " + person + ", today is " + date.toDateString() + "!"); } greet('Maddison', new Date()); With that, we have covered the bare-minimum basics you need to know of the TypeScript language. As you saw, it’s very much close to JavaScript so if you were already working with JavaScript then it should be easy to learn and migrate your projects to TypeScript. To make your work easy, we’ve created some dashboard templates. Check out now! Source: https://blog.wrappixel.com/introduction-to-typescript/ **********