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.

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  
}

Live Demo

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.

The Number Type

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

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

Live Demo

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.

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

Live Demo

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.

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

Live Demo

The Symbol Type

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

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

Live Demo

The Void Type

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

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

Live Demo

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.

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

Live Demo

The Undefined Type

The undefined datatype is used to uninitialized variables.

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

Live Demo

The Enum Types

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

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

Live Demo

Other types are covered in other sections


See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.