TypeScript Data types

By Dec 09, 2017

Description:

Here we present the data types of the Typescript language and we compare some with those of the JavaScript.

Preferencesoft

Basic Data types in TypeScript

Like many programming languages, TypeScript allows you to manipulate data.

The TypeScript language invites developers to specify the type of data used. It is not obligatory to specify the type of data, but this allows to detect at the level of editing, errors or inconsistencies in the code.

The TypeScript language recognizes several basic data types: number, string, boolean, array, tuple, enum, void, null, undefined, never, type assertions, class.

Note the absence in TypeScript of some types like integer types, long integer, character etc. It would have been pointless to add these data types since JavaScript stores integers in floating reals and represents a character by a single-character string.

Built-in types in TypeScript

Type Description
any Union of all other TypeScript types.
number represents double precision floating point values. There is no integer type and the integers are also represented in this form.
string collections of Unicode (utf-16) characters.
boolean represents the 2 logical values true and false. More exactly, the boolean type is the type {true, false, undefined, null}.
void Represents almost the empty type. In fact void is the type {undefined, null}. It can be considered as the complement of any. The type is used to indicate that a function does not return any value.
null null is the type {null, undefined}. It is a subtype of all other types. The value null represents a null object.
undefined undefined is the type {null, undefined}. It is a subtype of all other types. The value undefined is assigned to any uninitialized variable.
never never is the empty type {}. It is a subtype of all other types. It works to indicate that never a value will occur. A variable of type never will ever contain any value.
Warning! unassigned and null types are equal by definition, but unassigned and null values (which have the same names as types) do not have the same meaning when running a program.

User-defined types in TypeScript

Type Description
Array collection of data of the same type
Tuple collection of data that does not necessarily have the same type.
enum collections of named values

Let's add that there is TypeScript a type constructor type that allows you to create a new type from existing ones.

This would allow for example to pass functions of a certain type as a parameter of a function and thus to ensure that they correspond to a certain prototype.

type fun = (a: Number, b: Number) => Number;

Comparison with JavaScript

Some JavaScript types (number, String, boolean, null, undefined) can be found in TypeScript. But the language eclipses the object and function types because of their lack of precision. For example, the type function does not specify the prototype of a function.
TypeScript

Categories

Share

Follow


KodFor Privacy Policy