Typescript

Contents:
  • Introduction
  • Prerequisite & Environment set-up
  • Project Structure
  • ECMA-6 features
  • Data Types in ECMA-5 or JS
  • Custom types
  • Generic
  • Module
  • Decorators
  • Todo
Introduction:

Typescript=JavaScript + something-more

It is superset of java script.

Transpilers convert the TS to latest ECMAScript/JS version

JavaScript vs typescript

TS- statically typed language, you have to define the type before using it. So it detects the unavailable methods types before runtime moreover since typescript is a superset of JS, you can also use the dynamic nature of JS where you don’t have to define the type(by letting know ts to ignore Type), sine it has defined type it’s easy to understand.

JS- Dynamically typed language, prone to error but versatile in context of web-development being used in web-browser because sometimes we expect the type to be unknown.

Brief on JS-

Initially in 1996 Netscape navigator started using JS same time others too were using but of different version .so in 1997 ECMA standardized the JS hence JS is interchangeably called ES or ECMAScript

The latest version being used is ES6.

JS code

function func_name(value){                                 return a1+a2;                                                  }
var a1=10;
var a2=11;
var combine_value=a1+a2;
func_name(combine_value); //function call

Note:

  1. Above code is legal and returns sum of numbers

TS code :

function func_name(value: String){
return a1+a2;
}
var a1=10;
var a2=11;
var combine_value=a1+a2;
func_name(combine_value); //function call

Note:

  1. Illegal as function expects the string and we passed the number (to avoid this canuse combine_value=a1.toString()+a2)
  2. This kind of Type-check is not provided in JS

Serialization

Following rules are described-

  1. Introduction
  2. Item 85: Prefer alternatives to Java serialization
  3. Item 86: Implement Serializable with great caution
  4. Item 87: Consider using a custom serialized form
  5. Item 88: Write readObject methods defensively
  6. Item 89: For instance control, prefer enum types to readResolve
  7. Item 90: Consider serialization proxies instead of serialized instances

Introduction

JAVA – Object SERIALIZATION,
It is Java’s framework for encoding objects as byte streams (serialising) and reconstructing objects from their encoding (serialising). This mechanism is used to persist the object. This chapter focuses on the dangers of serialization and how to minimize them.

Concurrency

Objective

Concurrency canot be avoided it is inherent in platform(multicore) and requirement if you want to achieve better performance from multicore processors.
Motivation:
Concurrent programming is harder than single-threaded programming, because more things can go
wrong, and failures can be hard to reproduce.

Memory Map

Following rules are described

  1. Item 78: Synchronize access to shared mutable data . . . . . . . . . . 311
  2. Item 79: Avoid excessive synchronization . . . . . . . . . . . . . . . . . . 317
  3. Item 80: Prefer executors, tasks, and streams to threads . . . . . . . 323
  4. Item 81: Prefer concurrency utilities to wait and notify . . . . . . 325
  5. Item 82: Document thread safety . . . . . . . . . . . . . . . . . . . . . . . . . 330
  6. Item 83: Use lazy initialization judiciously . . . . . . . . . . . . . . . . . 333
  7. Item 84: Don’t depend on the thread scheduler . . . . . . . . . . . . . . 336

General Programming

Following rules are desscribed

  1. Item 57: Minimize the scope of local variables
  2. Item 58: Prefer for-each loops to traditional for loops
  3. Item 59: Know and use the libraries
  4. Item 60: Avoid float and double if exact answers are required
  5. Item 61: Prefer primitive types to boxed primitives
  6. Item 62: Avoid strings where other types are more appropriate
  7. Item 63: Beware the performance of string concatenation
  8. Item 64: Refer to objects by their interfaces
  9. Item 65: Prefer interfaces to reflection
  10. Item 66: Use native methods judiciously. .
  11. Item 67: Optimize judiciously
  12. Item 68: Adhere to generally accepted naming conventions .

Lambdas and Streams

Memory Map

Objective

Following rules are described

  1. Item 42: Prefer lambdas to anonymous classes
  2. Item 43: Prefer method references to lambdas
  3. Item 44: Favor the use of standard functional interfaces
  4. Item 45: Use streams judiciously
  5. Item 46: Prefer side-effect-free functions in streams
  6. Item 47: Prefer Collection to Stream as a return type
  7. Item 48: Use caution when making streams parallel

Generics

What is Generics:


Generic are introduced to programming language from v5, also it maintains support to Raw type.
Generic type is a class or interface followed by Parameter. the parameter is given in angular Bracket.
Generiic allow us to pass the type(Integer ,String ..user defined type) to method ,class or interface.

Parameter can be Formal or actual .
Formal Paramter is used when we declare the Generic type the actual parameter is used when we pass the values.

Why ?


It is better to detect the bug at earlist . the GENERICS provides the type safety so compiler complains when you to enter values other than one specified in generic parameter.
thus ensuring destructibility at compile time (rather than leaving it for run time error .)

Following rules are described –
  1. Item 26: Don’t use raw types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
  2. Item 27: Eliminate unchecked warnings. . . . . . . . . . . . . . . . . . . . 123
  3. Item 28: Prefer lists to arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
  4. Item 29: Favor generic types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
  5. Item 30: Favor generic methods . . . . . . . . . . . . . . . . . . . . . . . . . . 135
  6. Item 31: Use bounded wildcards to increase API flexibility . . . . 139
  7. Item 32: Combine generics and varargs judiciously. . . . . . . . . . . 146
  8. Item 33: Consider typesafe heterogeneous containers . . . . . . . . . 151

MemoryMap