EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Types
Secondary Sidebar
TypeScript Tutorial
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Types

By Priya PedamkarPriya Pedamkar

TypeScript-Types

Introduction to TypeScript Types

The following article provides an outline for various types of TypeScript. Every Programming language is based on some keywords and datatypes. We have seen many types of data. The concept is so simple. We know that the alphabets are the one and numbers. For storing each type of data in proper way programming languages have separated them with some names.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,198 ratings)

Different Types of TypeScript

Don’t worry if you are new to these words. But if you are from a programming background then it is very easy to understand. In the coming section, we are looking at each of these in detail:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of TypeScript

1. Number

As we know TypeScript is a superset of JavaScript. So, Javascript doesn’t have data types like integer and float. Typescript belongs to the same concept. It has only one numeric type and i.e Number. But note that every number in typescript is a kind of floating-point.

Example

let marks: number;

We can also consider that for all types of no’s like float, double, int, long int, etc we have the only number as a data type.

2. String

As we’ve seen, the number datatype is just one like we have the string datatype. Any character or collection of characters is a string. This is one of the important data types we have in TypeScript. We can make use of both single quotes (‘ ‘) and double quotes(“ “). Template string is a new concept in Typescript.

Example

name: string = 'John';
Designation: string = "Engineer";

3. Enum

This is a very important one. It is more like an array. We can give a suitable name to the given values. Also, specify the index no.

Example

enum Fruits {apple = 2, banana, kiwi}
let favFruit: string = Fruits[3];

Look at the above code snippet. It has fruits and we decided to start its index at 2. Then the output will be banana.

4. Boolean

It is the simplest data type as we all know it gives only two values i.e. true and false.

Example

let isSuccess: boolean = true;

We are using this data type mostly on the condition-based rendering of the code.

5. Any

Any data type is very useful in programming at a time when we are not aware that what kind of data will be there. In many cases, we are unknown to the king of data being used. So, in these situations, we are giving it as Any. We can use this type of mixed array also.

Example

data: any;

When we are using a javascript framework like angular or react at that time we don’t know the type of data we get from a third party or an API. So at this instance of time, we can use any data type.

6. Void

Void is nothing but an empty thing. There is no data type for the given data. We are not using it on variables rather we use it on function as if the function does not have anything to return.

Example

function message(): void {
console.log("Welcome!!!");
}

7. Never

Like void never is also used on functions that never return anything or may go into the exception.

Example

Function abc() :never{……some code….}

Many times we have to handle the exception where code is not able to run or any error occurs. These functions are not returning any valuable data. In these scenarios, we can use never data type.

8. Array

Same like JavaScript, typescript also has array of data types. There are two ways to declare arrays in typescript. The array is nonprimitive or we can say reference type or user-defined data type. Every programming language has an array as its data type. It is considered as one of the important data types.

Example

let name:string[] = ['john','sam']
Letname: Array<string>= ['john','sam']

In the above example, we are specifying the type of array which is a string. In typescript, we mostly use angle brackets to specify the type.

9. Object

Array and object are reference types mainly i.e also known as user-defined or non-primitive.

Example

const obj = {}

10. Type assertions

Type assertion is nothing but type casting or type cohesion in other languages.

Example

let name: any = "Sam";
let nameNo: number = (<string> name).length;

In the above example, we have one variable called the name of type any.

On the second line as you see we have another variable nameno which is having a type number. Because we need string but need to get the length of our variable too.

11. Null and Undefined

These are the two most important and most confusing terms in typescript. These are also in javascript. If you are familiar with these then ok. If not don’t worry we will check this out.

  • Null: Null is we are using in a situation where we have to give value to the variable but not now. Maybe in the future. But today we have to give it some value in this situation we are giving null to that variable as a type.
  • Undefined: This is the data type that is by default get applied to all variables in javaScript. It is the same as void we can say.

Suppose we declared on variable and have not provided any value to it then by default it gets undefined as its datatype and value also.

12. Tuple

Tuple is nothing but the array which is having data of its data type with it.

Example

Let employee: [string, number];
employee= ["sam",101];

In the above example, we have an array named employee which will be having one value of type string and is of type number.

Conclusion

Typescript is mainly recognized for its strictly typing. JavaScript has dynamic typing which leads to some security issues. At this point, Typescript comes to save us from this drawback.

Recommended Articles

This has been a guide to TypeScript Types. Here we discuss the introduction and different types of TypeScript with examples. You may also have a look at the following articles to learn more –

  1. TypeScript Versions
  2. How to Install Typescript
  3. TypeScript Operators
  4. TypeScript Functions
Popular Course in this category
JavaScript Training Program (39 Courses, 24 Projects, 4 Quizzes)
  39 Online Courses |  24 Hands-on Projects |  230+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more