Basic Types
Boolean
The most basic data type is the simple true/false value, which JavaScript and TypeScript call a boolean
value.
Number
As in JavaScript, all numbers in TypeScript are either floating point values or BigIntegers. These floating point numbers get the type number
, while BigIntegers get the type bigint
. In addition to hexadecimal and decimal literals, TypeScript also supports binary and octal literals introduced in ECMAScript 2015.
String
Another fundamental part of creating programs in JavaScript for webpages and servers alike is working with textual data. As in other languages, we use the type string
to refer to these textual datatypes. Just like JavaScript, TypeScript also uses double quotes ("
) or single quotes ('
) to surround string data.
Template string
You can also use template strings, which can span multiple lines and have embedded expressions. These strings are surrounded by the backtick/backquote (`
) character, and embedded expressions are of the form ${ expr }
.
Array
TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by []
to denote an array of that element type:
The second way uses a generic array type, Array<elemType>
:
Tuple
Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same. For example, you may want to represent a value as a pair of a string
and a number
:
When accessing an element with a known index, the correct type is retrieved:
Enum
A helpful addition to the standard set of datatypes from JavaScript is the enum
. As in languages like C#, an enum is a way of giving more friendly names to sets of numeric values.
Last updated