Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
Difference between TypeScript and JavaScript
Next article icon

Introduction to TypeScript

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript is a syntactic superset of JavaScript that adds optional static typing, making it easier to write and maintain large-scale applications.

  • Allows developers to catch errors during development rather than at runtime, improving code reliability.
  • Enhances code readability and maintainability with features like type annotations and interfaces.
  • Fully compatible with JavaScript, enabling seamless integration with existing projects.
  • It is ideal for large-scale applications where strict type-checking and better tooling are essential.

What is TypeScript?

TypeScript is essentially JavaScript with additional features, most notably the ability to use type annotations. While JavaScript is dynamically typed, meaning types are determined at runtime, TypeScript allows developers to define types at compile time. This can help catch errors early in the development process and makes code easier to understand and maintain.

Key Features of TypeScript

1. Static Type Checking (Optional)

TypeScript allows you to check and assign types to variables, parameters, and function return values. While this step requires a little more effort, it significantly improves code quality. Optional static typing helps prevent bugs and makes your code more readable.

2. Class-Based Objects

One of TypeScript’s standout features is its support for classes. Unlike JavaScript’s prototype-based approach, TypeScript lets you write true object-oriented code. You can create classes, define constructors, and use inheritance and access modifiers (public, private, protected).

3. Modularity

TypeScript promotes modularity. By using modules, you can organize your code into smaller, reusable pieces. This modularity enhances maintainability and collaboration among team members.

4. ES6 Features

TypeScript embraces ECMAScript 6 (ES6) features. If you’re familiar with ES6 syntax (arrow functions, template literals, destructuring, etc.), you’ll feel right at home with TypeScript.

5. Syntactical Sugaring

TypeScript’s syntax is closer to that of high-level languages like Java. It’s like a sweetener for developers—more concise and expressive.

Structure of TypeScript

TypeScript Code cannot be interpreted by the Browser directly so there is a need to compile the TypeScript code into plain JavaScript Code, for this purpose we need the TypeScript Compiler (tsc).

Structure of TypeScript

TypeScript Compiler (tsc)

  • Written in TypeScript itself.
  • Compiles .ts files to .js files.
  • Installed as an NPM package (NodeJS).
  • Supports ES6 syntax.

TypeScript vs. JavaScript

FeatureTypeScriptJavaScript
TypingStatically TypedDynamically Typed
Object OrientationClass-BasedPrototype-Based
ModulesSupports ModulesDoes not Support Modules
Error DetectionCompile-Time ErrorsRuntime Errors
CompilationRequires CompilationInterpreted by Browsers/Node.js
Code MaintainabilityEasier due to static typing and interfacesCan be harder in large codebases
Tooling SupportAdvanced (autocompletion, refactoring)Basic
Use CasesLarge projects, complex applicationsSmall to medium projects, quick prototyping

Why TypeScript is Gaining Popularity ?

  • Enhanced Code Quality: Static typing and interfaces lead to more robust and maintainable code.
  • Developer Experience: Improved tooling support provides a richer environment for spotting errors during development.
  • Framework Adoption: Popular frameworks like Angular have adopted TypeScript, contributing to its rising popularity.
  • Active Community: Used by top tech companies, ensuring continuous improvement and support.

Why Do We Use TypeScript ?

  1. Better developer experience – One of the biggest advantages of TypeScript is to enable IDEs to provide a richer environment for spotting common errors as you type the code. For a large scale project adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.
  2. Code quality – Defining data structures in the beginning, using types and interfaces, forces you to think about your app’s data structure from the start and make better design decisions.
  3. Prevents bugs – TypeScript won’t make your software bug free. But it can prevent a lot of type-related errors. Along with the Clever IntelliSense many browsers and IDEs support direct debugging through Source Maps.
  4. Active community – TypeScript is getting more and more popular. It’s used by the top tech companies like Google, Airbnb, Shopify, Asana, Adobe, and Mozilla so we can assume that it reaches their expectations in terms of scalability – as they are developing large and complex applications.
  5. TypeScript Is Just JavaScript – TypeScript starts with JavaScript and ends with JavaScript. Typescript adopts the basic building blocks of your program from JavaScript. All TypeScript code is converted into its JavaScript equivalent for the purpose of execution.

How do I use TypeScript?

A common way to use TypeScript is by utilizing the official TypeScript compiler, which transpiles TypeScript code into JavaScript. To get started, you’ll need to set up the compiler for your local project. Many popular code editors, such as Visual Studio Code, have built-in TypeScript support, providing real-time error detection and suggestions as you write your code. This integration makes it easier to work with TypeScript, ensuring you catch potential issues early in the development process.

Steps for Adding TypeScript File in HTML Code

1. Create the TypeScript File (types.ts)

JavaScript
let myString; myString = 'Hello from ts'; console.log(myString); 
  • myString is declared as a string variable.
  • It’s assigned the value ‘Hello from TypeScript’.
  • The value is logged to the console.

2. Compile TypeScript to JavaScript

Use the TypeScript compiler (tsc) to transpile types.ts into JavaScript. Open your terminal and run:

tsc types.ts

This command generates a types.js file containing the equivalent JavaScript code.

3. Create the HTML File (index.html)

HTML
<html lang="en"> <head> </head> <body>     <h2>Welcome to GeeksforGeeks</h2>     <p>Default code has been loaded into the Editor.</p>     <script src="types.js"></script> </body> </html> 
  • A heading and a paragraph for content.
  • A script tag that references the compiled JavaScript file types.js.

4. Run the HTML File

Open index.html in a web browser.

5. Output

Introduction to TypeScript - Output

Next Article
Difference between TypeScript and JavaScript
author
anshubajaj911
Improve
Article Tags :
  • JavaScript
  • TypeScript
  • Web Technologies

Similar Reads

  • TypeScript Tutorial
    TypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications. Static typing allows you to
    8 min read
  • TypeScript Basics

    • Introduction to TypeScript
      TypeScript is a syntactic superset of JavaScript that adds optional static typing, making it easier to write and maintain large-scale applications. Allows developers to catch errors during development rather than at runtime, improving code reliability.Enhances code readability and maintainability wi
      5 min read

    • Difference between TypeScript and JavaScript
      Ever wondered about the difference between JavaScript and TypeScript? If you're into web development, knowing these two languages is super important. They might seem alike, but they're actually pretty different and can affect how you code and build stuff online. In this article, we'll break down the
      4 min read

    • How to install TypeScript ?
      TypeScript is a powerful language that enhances JavaScript by adding static type checking, enabling developers to catch errors during development rather than at runtime. As a strict superset of JavaScript, TypeScript allows you to write plain JavaScript with optional extra features. This guide will
      3 min read

    • Hello World in TypeScript
      TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows javascript syntactically but adds more features to it. It is a superset of javascript. The diagram below depicts the relationship: Typescript is purely object-oriented with features lik
      3 min read

    • How to execute TypeScript file using command line?
      TypeScript is a statically-typed superset of JavaScript that adds optional type annotations and compiles to plain JavaScript. It helps catch errors during development. To execute a TypeScript file from the command line, compile it using tsc filename.ts, then run the output JavaScript file with node.
      2 min read

    • Variables in TypeScript
      Variables in TypeScript are used to store data values, acting as named memory locations that can hold numbers, strings, booleans, or other types of data. Variables can be declared using let, const, or var depending on the use case.They provide type safety by allowing you to define specific data type
      4 min read

    • What are the different keywords to declare variables in TypeScript ?
      Typescript variable declarations are similar to Javascript. Each keyword has a specific scope. Let's learn about variable declarations in this article. In Typescript variables can be declared by using the following keywords: varlet constVar keyword: Declaring a variable using the var keyword. var va
      4 min read

    • Identifiers and Keywords in TypeScript
      In TypeScript, identifiers are names used for variables, classes, or methods and must follow specific naming rules. Keywords are reserved words with predefined meanings and cannot be used as identifiers. Comments, both single-line and multi-line, enhance code readability and are ignored during code
      2 min read

    TypeScript primitive types

    • Data types in TypeScript
      In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity. Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Pri
      3 min read

    • TypeScript Numbers
      TypeScript Numbers refer to the numerical data type in TypeScript, encompassing integers and floating-point values. The Number class in TypeScript provides methods and properties for manipulating these values, allowing for precise arithmetic operations and formatting, enhancing JavaScript's native n
      4 min read

    • TypeScript String
      In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string. TypeSc
      4 min read

    • Explain the concept of null and its uses in TypeScript
      Null refers to a value that is either empty or a value that doesn't exist. It's on purpose that there's no value here. TypeScript does not make a variable null by default. By default unassigned variables or variables which are declared without being initialized are 'undefined'. To make a variable nu
      3 min read

    TypeScript Object types

    • What are TypeScript Interfaces?
      TypeScript interfaces define the structure of objects by specifying property types and method signatures, ensuring consistent shapes and enhancing code clarity. Allow for optional and read-only properties for flexibility and immutability.Enable interface inheritance to create reusable and extendable
      4 min read

    • TypeScript class
      A TypeScript class is a blueprint for creating objects, encapsulating properties (data) and methods (behavior) to promote organization, reusability, and readability. Supports inheritance, allowing one class to extend another and reuse functionality.Provides access modifiers (public, private, protect
      4 min read

    • How enums works in TypeScript ?
      In this article, we will try to understand all the facts which are associated with enums in TypeScript. TypeScript enum: TypeScript enums allow us to define or declare a set of named constants i.e. a collection of related values which could either be in the form of a string or number or any other da
      4 min read

    • TypeScript Tuples
      In JavaScript, arrays consist of values of the same type, but sometimes we need to store a collection of values of different types in a single variable. TypeScript offers tuples for this purpose. Tuples are similar to structures in C programming and can be passed as parameters in function calls. Tup
      4 min read

    TypeScript other types

    • What is any type, and when to use it in TypeScript ?
      Any is a data type in TypeScript. Any type is used when we deal with third-party programs and expect any variable but we don't know the exact type of variable. Any data type is used because it helps in opt-in and opt-out of type checking during compilation.  In this article, we will see what is any
      3 min read

    • How to Create an Object in TypeScript?
      TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data. Creating Objects in TypescriptNow, let
      4 min read

    • What is an unknown type and when to use it in TypeScript ?
      In Typescript, any value can be assigned to unknown, but without a type assertion, unknown can't be assigned to anything but itself and any. Similarly, no operations on an unknown are allowed without first asserting or restricting it down to a more precise type.  similar to any, we can assign any va
      3 min read

    • Explain the purpose of never type in TypeScript
      In Typescript when we are certain that a particular situation will never happen, we use the never type. For example, suppose you construct a function that never returns or always throws an exception then we can use the never type on that function. Never is a new type in TypeScript that denotes value
      3 min read

    TypeScript combining types

    • TypeScript Union
      The TypeScript union has the ability to combine one or two different types of data (i.e., number, string, float, double, etc). It is the most powerful way to express a variable with multiple types. Use pipe ('|') symbol to combine two or more data types to achieve Union type. Syntax: (type1|type2|ty
      3 min read

    • What are type aliases and how to create it in Typescript ?
      In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't
      3 min read

    TypeScript Assertions

    • Explain Type assertions in TypeScript
      In TypeScript, type assertions allow developers to override the compiler's inferred type, informing it of the specific type of a value. Type assertions are purely compile-time constructs and do not alter the runtime behavior of the code. They are particularly useful when interfacing with APIs or thi
      4 min read

    TypeScript Functions

    • How to write a function in Typescript ?
      Writing a function in TypeScript is similar to writing it in JavaScript but with added parameters and return type. Note that any JavaScript function is a perfectly valid TypeScript function. However, we can do better by adding type. Syntax: Let's see a basic TypeScript function syntax (with two argu
      4 min read

    • How to achieve function overloading in TypeScript ?
      In this article, we will try to understand some basic details which are associated with the concept of function/method overloading, further will see how we could implement function overloading in TypeScript. Let us first understand some basic facts involved in function/method Overloading. Function/M
      2 min read

    • Explain the arrow function syntax in TypeScript
      Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The main addition in TypeScript is the inclusion of data types or return types in the function syntax, along with the types for the arguments passed into the function. What is arrow function syntax in TypeScript?Arrow funct
      3 min read

    • TypeScript toPrecision() Function
      The toPrecision() method is used to return the string representation in exponential or fixed-point to the specified precision. Syntax:number.toPrecision( [ precision ] )Parameters:It represents an integer value specifying the number of significant digits. Return Value:The toPrecision() method in Typ
      1 min read

    • TypeScript toFixed() Function
      The toFixed() function in TypeScript formats a number using fixed-point notation, specifying the number of digits after the decimal point. It returns a string representation of the number, ensuring precise control over its decimal places for consistent numerical formatting. Syntaxnumber.toFixed( [di
      2 min read

    • TypeScript toLocaleString() Function
      The toLocaleString() function in TypeScript converts a number to a locale-specific string representation. It optionally accepts locale and formatting options to customize the output, such as currency or decimal precision, ensuring numbers are formatted according to regional conventions. Syntaxnumber
      2 min read

    • TypeScript toString()
      The toString() method in TypeScript is used to return a string representing the specified object radix (base). Syntax:number.toString( [radix] )Parameters:This function accepts a single parameter as mentioned above and described below. radix: This parameter represents an integer between 2 and 36 spe
      1 min read

    TypeScript interfaces and aliases

    • What are TypeScript Interfaces?
      TypeScript interfaces define the structure of objects by specifying property types and method signatures, ensuring consistent shapes and enhancing code clarity. Allow for optional and read-only properties for flexibility and immutability.Enable interface inheritance to create reusable and extendable
      4 min read

    • What are type aliases and how to create it in Typescript ?
      In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't
      3 min read

    TypeScript classes

    • How to Extend an Interface from a class in TypeScript ?
      In this article, we will try to understand how we to extend an interface from a class in TypeScript with the help of certain coding examples. Let us first quickly understand how we can create a class as well as an interface in TypeScript using the following mentioned syntaxes: Syntax:  This is the s
      3 min read

    • How to Create an Object in TypeScript?
      TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data. Creating Objects in TypescriptNow, let
      4 min read

    • How to use getters/setters in TypeScript ?
      In TypeScript, getters and setters provide controlled access to class properties, enhancing encapsulation and flexibility. Getters allow you to retrieve the value of a property with controlled logic.Setters enable controlled assignment to properties, often including validation or transformations.[GF
      5 min read

    • TypeScript Inheritance
      Inheritance is a fundamental concept in object-oriented programming (OOP). It allows one class to inherit properties and methods from another class. The class that inherits is called the child class, and the class whose properties and methods are inherited is called the parent class. Inheritance ena
      3 min read

    • When to use interfaces and when to use classes in TypeScript ?
      TypeScript supports object-oriented programming features like classes and interfaces etc. classes are the skeletons for the object. it encapsulates the data which is used in objects. Interfaces are just like types for classes in TypeScript. It is used for type checking. It only contains the declarat
      4 min read

    • Generics Interface in typescript
      "A major part of software engineering is building components that not only have well-defined and consistent APIs but are also reusable. " This sentence is in the official documentation we would start with. There are languages that are strong in static typing & others that are weak in dynamic typ
      5 min read

    • How to use property decorators in TypeScript ?
      Decorators are a way of wrapping an existing piece of code with desired values and functionality to create a new modified version of it. Currently, it is supported only for a class and its components as mentioned below: Class itselfClass MethodClass PropertyObject Accessor ( Getter And Setter ) Of C
      4 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences