Javatpoint Logo
Javatpoint Logo

TypeScript Type

The TypeScripts language supports different types of values. It provides data types for the JavaScript in order to transform it into a strongly typed programing language. JavaScript doesn't support data types, but with the help of TypeScript, we can use the data types feature in JavaScript. As every object-oriented programmer wants to use the type feature in any language either it is a scripting language or object-oriented programming language, TypeScript plays an important role. The Type System checks the validity of the given values before the program uses them. It ensures that the code behaves as expected.

TypeScript provides data types as an optional Type System. The TypeScript data type classification is given below-

TypeScript Type

1. Static Types

In the context of type systems, static types mean "at compile time" or "without running a program." In a statically typed language, variables, parameters, and objects have types that the compiler knows at compile time. The compiler used this information to perform the type checking.

Static types can be further divided into two sub-categories:

Built-in or Primitive Type

The TypeScript has 5 built-in data types. These are:

TypeScript Type

Number

Like JavaScript, all the numbers in TypeScript are stored as floating point values. These numeric values are treated as a number data type. The number data type can be used to represents both, integers and fractions. TypeScript also supports Binary(Base 2), Octal(Base 8), Decimal(Base 10), and Hexadecimal(Base 16) literals.

Syntax:

Examples:-

String

We will use the string data type to represents the text in TypeScript. String type work with textual data. We include string literals in our scripts by enclosing them in single or double quotation marks. Double quotation marks can be contained in strings surrounded by single quotation marks, and single quotation marks can be contained in strings surrounded by double quotation marks. It also represents a sequence of Unicode characters. It embedded the expressions in the form of $ {expr}.

Syntax:

Examples:-

Boolean

The string and numeric data types can have a virtually unlimited number of different values, whereas the Boolean data type can have only two values. They are true and false. A Boolean value is a truth value that specifies whether the condition is true or not.

Syntax:

Examples:-

Void

A void is the return type of the functions that do not return any type of value. It is used where no datatype is available. A variable of type void is not useful because we can only assign undefined or null to them. Undefined datatype denotes uninitialized variable whereas null represents a variable whose value is undefined.

Syntax:

Examples:-

Null

Null represents a variable whose value is undefined. Much like the void, it is not extremely useful on their own. The Null accepts the one and only value null. The Null keyword is used to define the Null type in TypeScript, but it is not useful because we can only assign a null value to it.

Examples:-

Undefined

The Undefined primitive type denotes all uninitialized variables in TypeScript and JavaScript. It has one and only value that is undefined. The undefined keyword defines Undefined type in TypeScript, but it is not useful because we can only assign an undefined value to it.

Example:

Any Type

It is the super type of all datatype in TypeScript. It is used to represents any JavaScript value. It allows us to opt-in and opt-out of type-checking during compilation. If a variable cannot be represented in any of the basic data types, then it can be declared using Any datatype. Any type is useful when we do not know about the type of value (which might come from an API or 3rd party library), and we want to skip the type-checking on compile time.

Syntax:

Examples:-

User-Defined DataType

TypeScript supports following user-defined datatypes:

TypeScript Type

Array

An array is a collection of elements of the same datatype. Like JavaScript, TypeScript also allows us to work with arrays of values. An array can be written in two ways:

1. Use the type of the elements followed by [] to denote an array of that element type:

2. The second way uses a generic array type:

Touple

The Tuple is a data type which includes two sets of values of different data types. It allows us to express an array where the type of a fixed number of elements is known, but they are not the same. For example, if we want to represent a value as a pair of a number and a string, then it can be written as:

Interface

An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class that implements an interface is bound to implement all its members. It cannot be instantiated but can be referenced by the class that implements it. The TypeScript compiler uses interface for type-checking that is also known as "duck typing" or "structural subtyping."

Example:

Class

Classes are used to create reusable components. It acts as a template for creating objects. It is a logical entity that stored variables and functions to performs operations. TypeScript gets support for classes from ES6. It is different from the interface that has an implementation inside it whereas an interface does not have any implementation inside it.

Example:

Enums

Enums define a set of named constant. TypeScript provides both string-based and numeric-based enums. By default, enums begin numbering their elements starting from 0, but we can also change this by manually setting the value to one of its elements. TypeScript gets support for enums from ES6.

Example:

Functions

A function is the logical blocks of code to organize the program. Like JavaScript, TypeScript can also be used to create functions either as a named function or as an anonymous function. Functions ensure that our program is readable, maintainable and reusable. A function declaration has a function's name, return type, and parameters.

Example:


2. Generic

Generic is used to create a component that can work with a variety of datatype rather than a single one. It allows a way to create reusable components. It ensures that the program is flexible as well as scalable in the long term. TypeScript uses generics with the type variable <T> that denotes types. The type of generic functions is just like non-generic functions, with the type parameters listed first, similarly to function declarations.

Example:


3. Decorators

A decorator is a special kind of datatype that can be attached to a class declaration, method, property, accessor, and parameter. It provides a way to add both annotations and a meta-programing syntax for classes and functions. It is used with "@" symbol.

A decorator is an experimental feature that may change in future releases. To enable support for the decorator, we must enable the experimentalDecorators compiler option either on the command line or in our tsconfig.json.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA