TypeScript Basic Types

Like any other languages, TypeScript types are divided into different categories. All types in TypeScript are subtypes of a single top type called the Any type.

Any Type

The Any type is representing any JavaScript value with no constraints. If you don't know about the types, you can use any type to hold any type that are assigning dynamically.

TypeScript Types Example 1

let myVar: any = 4;
myVar = "can be string";
myVar = false; // can be boolean

var a: any;             // Explicitly typed  
var b;                  // Same as y: any  
var c: { x; y; };       // Same as z: { a: any; b: any; }

function foo(a) {         // Same as foo(a: any): void  
}
								
Try it Yourself

The other types in TypeScript are categorized into

  • Primitive Types
  • Object Types
  • Union Types
  • Intersection Types
  • Types parameters

Primitive Types

The primitive types includes Number, Boolean, String, Symbol, Void, Null, and Undefined types along with user defined enum types. Each primitive type uses specific keywords in programming such as number, boolean, string, symbol, and void respectively.

1. The Number Type

The number datatype is a primitive type and represents double-precision or floating point values.

TypeScript Number Type Example 2

var a: number;          // Explicitly typed  
var b = 0;              // Similar to b: number = 0  
var c = 123.456;        // Similar to c: number = 123.456  
var d = z.toFixed(2);   // Property of Number interface
								
Try it Yourself

2. The Boolean Type

This is very simple datatype which is used to hold true/false value and both JavaScript and TypeScript call as boolean value.

TypeScript Boolean Type Example 3

var a: boolean;         // Explicitly typed  
var isValid = true;         // Similar to isValid: boolean = true  
var isInValid = false;         // Similar to isInValid: boolean = false
								
Try it Yourself

3. The String Type

The string datatype, like other languages is used to hold text data with double quotes (") or single quotes (') to surround text data.

TypeScript String Type Example 3

var str: string;          // Explicitly typed  
var empty = "";         // Similar to empty: string = ""  
var str = 'abc';        // Similar to str: string = "abc"  
var c = abc.charAt(2);  // Property of String interface
								
Try it Yourself

4. The Symbol Type

The symbol datatype is primitive type and represents unique tokens that may be used as keys for object properties.

TypeScript Symbol Type Example 3

var idKey = Symbol();  
var obj = {};  
obj[idKey] = "hidden message";  // Use symbol as property key  
obj[Symbol.toStringTag] = "test";   // Use of well-known symbol
								
Try it Yourself

5. The Void Type

The void datatype is represent absence of having any type and commonly used as return type.

TypeScript Void Type Example 3

function displayMsg(): void {
    alert("This is my warning message");
}
let unusable: void = undefined;
								
Try it Yourself

6. The Null Type

The null datatype is references the one and only value of the Null type and considered a valid value for all primitive types.

TypeScript Null Type Example 3

var a: number = null;   // Primitives can be null  
var b = null;           // Similar to b: any = null  
var c: Null;            // Error, can't reference Null type
								
Try it Yourself

7. The Undefined Type

The undefined datatype is used to uninitialized variables.

TypeScript Undefined Type Example 3

var a: number;          // Same as a: number = undefined  
var b = undefined;      // Same as b: any = undefined  
var c: Undefined;       // Error, can't reference Undefined type
								
Try it Yourself

8. The Enum Type

The enum datatype, like other languages, is used to give more friendly names to sets of numeric values.

TypeScript Enum Type Example 3

enum Fruits {Orange, Banana, Mango, Apple};
let a: Fruits = Fruits.Orange;
								
Try it Yourself

Other types are covered in other sections