Types and variables
TypeScript is a programming language that supports (optional) static types with support for type inference. Types define constraints on elements of your program (variables, functions, classes etc.)that enable the compiler and various tools (like IDEs) to support you with verification and assistance in writing your programs.
Two key points to note:
- All of the typescript type analysis is done at compile time. At run time everthing is transpiled to JavaScript. So there are no additional features or overhead at run time.
- It is very interesting to observe how closely the TypeScript language construction tries to mirror the behavior of JavaScript, especially its dynamic nature and support for proptotypes.
To get started I encourage you to use the Typescript playground. This will allow you to try Typescript without any installs. We can install the Typescript compiler and tools later. In the playground you can see the JavaScript that the Typescript compiler outputs, which is helpful if you are familiar with JavaScript. You can also run the code to see the outputs.
The type Any
All the types in TypeScript are sub-types of the type Any represented by the keyword any
. The type Any represents a type with no type constraints.
Here are some examples of the usage of the type any in explicit annotation and in situation where the inferred type is any
.
var x:any;
var y;
function echo(x){
return x;
}
The other types are categorized as:
- primitive types
- object types
- union types
- intersection types
- type parameters
In this section we will focus on the primitive types. In TypeScript they are:
- Number
- Boolean
- String
- Symbol
- void, null and undefined
There are also user defined enums
that we will cover later.
If you are familiar with JavaScript you will see that this the basic type in TypeScript are very similar to the basic types in JavaScript.
In TypeScript you can extend the simple Basic types by creating object types which are:
- function
- class
- interface
- array
- tuple
We will analyze these in detail with examples in seperate chapters.
Variables and Declarations
Declarations associate variables with types (and as you will see later also functions, classes etc with types). The declarations can use explicit type annotations or let the compiler use type inference to associate types with variables. The main point is that types will be statically assigned to variables.
In TypeScript you declare variables with the keywords let
, var
and const
. The main differences between these three different forms have to do with the scope and the mutability of the variable. We will cover this in more details later but for now we will use the var
form for declarations in this section since it is familiar to JavaScript developers.
A general variable declaration will look like:
var <variableName>:optionalType = <anExpression>;
or
var <variableName> = <anExpression>;
In the first case we are explicitly specifying the type that we want the variable to have. In the second case the TypeScript compiler will infer the type of the variable based on the expression on the right hand side of the assignment. It is a very interesting personal choice as to whether you want to be explicit or not. As you get more familiar working with the compiler and depending on your code you will find the correct balance between being explicit and using optional typing.
Number
The number
primitive type represents double-precision 64-bit format IEEE 754 floating point values. (Similar to JavaScript). It is represented by the number
keyword.
Here are some examples
let aNum=3 //a:number is inferred and assigned to aNum.
let a:number =3; //Explicitly typed
let pi=3.141;
If you try this in the playground you can see a tool-tip with the inferred type assigned by the compiler when you hover over the variable.
Or you can use an alert or log call using the typeof
keyword to check the inferred type.
var aNum = 3;
alert(typeof aNum);
//console.log(typeof aNum);
Boolean
The Boolean primitive type represents the logical values that are true
or false
. In TypeScript they are represented by the keyword boolean
and the logical values by the keywordstrue
and false
respectively.
Here are some examples:
var aboolean = true; //inferred
var anotherBoolean:boolean = true; //explicit
String
The String type represents sequences of characters stored as Unicode UTF-16 code units. It is represented by the keyword string
in TypeScript.
var aString="Hello";
var anotherString:string="World";
var emptyString ="";
The elements of the string can be enclosed in single or double quotes.
Undefined
The type Undefined is a sub-type of all types and is represented by the literal undefined
.
The only value of type Undefined is the literal undefined
. The Undefined type is a subtype of all types. undefined
is the value given to uninitialized variables of all types.
var a;
var b=undefined;
//var c:undefined; //Error
The Void type
The Void type is used in situations where you want to represent the absence of a value. For example if you have a function that does not return a value.
The Void type is a subtype of any
and a super type of null
and undefined
.
The only possible values of a void type are null
or undefined
.
The Null type
The Null type corresponds to the null
primitive in JavaScript. The null
literal is the only value that this type can take.
var aNullValue:any = null;
var aNumber:number = null;
The Symbol type
The Symbol type corresponds to the Symbol type in JavaScript. It is used to create tokens that can be used as keys for object properties.
var aSymbol = Symbol();
var dataStore = {}
dataStore[aSymbol]="Some data";
This allows you to let the compiler help spot errors that arise from mis-typing when you use strings.