TypeScript supports type checking for all primitive types and object types. TypeScript also supports generics, decorators, and ES6 module types as well. It is important to learn that the type system in typescript is designed to be optional. So all programs written in JavaScript are TypeScript programs as …
TypeScript supports type checking for all primitive types and object types. TypeScript also supports generics, decorators, and ES6 module types as well.
It is important to learn that the type system in typescript is designed to be optional. So all programs written in JavaScript are TypeScript programs as well.
In the context of language semantics and type systems:
Static types usually mean “at compile time” or “without running a program”
Dynamic types mean “at runtime”
In a statically typed language (e.g. TypeScript), variables, parameters, and members of objects have types that the compiler knows at compile time. The compiler can use that information to perform type checks and to optimize the compiled code.
Static types in TypeScript can be divided into two sub-categories:
1.1. Primitive Types
TypeScript has 5 primary primitive types –
number
string
boolean
void
any
Primitive Type
Description
number
Use to define variable of type number.
letnum: number = 123;
num = 123.456;
num = '123'; // Error
string
Use to define variable of type string.
string 1=example
letstr: string = 'hello';
str = 'world';
str = 123; // Error
boolean
Use to define variable of type boolean.
letbool: boolean = false;
bool = true;
bool = 123; // Error
void
Used on function return types to represent non-returning functions.
void 1=example
functionwarnUser(): void {
alert("This is my warning message");
}
Declaring variables of type void is not useful because you can only assign undefined or null to them.
lettempVar: void = undefined;
tempVar = null;
tempVar = 123; //Error
any
Use any when you want to opt-out of type-checking and let the values pass through compile-time checks.
letval: any = 'hello';
val = 123; // OK
val = true; // OK
1.2. Object Types
TypeScript supports the following object types.
Object Type
Description
Array
An array is a collection of values of the same datatype.
Interfaces define properties, methods, and events which deriving member classes must implement.
interfaceICalc {
add (first: number, second: number): any;
}
letCalculator: ICalc = {
add(first: number, second: number) {
returnfirst + second;
}
}
Class
A class is a template for creating objects. Typescript gets support for classes from ES6.
classPerson {
//field
name:string;
//constructor
constructor(name:string) {
this.name = name;
}
//function
speakName():void {
console.log("Name is : "+this.name)
}
}
Enum
Like other programming languages, an Enum is a datatype consisting of a set of named values. The names are usually identifiers that behave like constants. Enums were introduced in ES6.
Enum 1=example
enumDirection {
Up,
Down,
Left,
Right
}
letgo: Direction;
go = Direction.Up;
Function
In TypeScript, we can declare variables who will point to only functions in their lifetime.
Function 1=example
letfun: Function = () => console.log("Hello");
fun = 123; //Error
2. Generic Types
Generics allows creating a component that can work over a variety of types rather than only a single type.
Generics Example
functionthrowBack<T>(arg: T): T { //Function return the parameter as it is
returnarg;
}
letoutputStr = identity<string>("myString"); //OK
letoutputNum = identity<number>( 100 ); //OK
3. Decorators
In a general sense, decorators are annotations. They are used with '@' symbol. Decorators allows us to decorate classes and functions, similar to annotations in Java and decorators in Python.
Decorators are a new feature that will probably make it into the ES7 version of JavaScript. However the functionality is available in TypeScript (experimental), so we can already make use of it.
3.1. How to use a Decorator
It is important to learn that every decorator is javascript function. To create decorator, create function like this:
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments