TypeScript Features:
- Superset of JavaScript
- Types add safety - As name indicates - Compile time type checking - Enables faster development
- Compiles to plain JavaScript
- Cross-platform and Open source
Declaring Variables:
- var:
a. Globally available in the function in which it is declared
b. "Hoisted" to the top of the function
c. can define same variable again - let and const: a. Only available in the block which it is decared b. Not "Hoisted" to top of the block c. Not possible declare the same variable again
Basic DataTypes:
- Boolean
- Number
- String
- Array
- Enum
- Any
- Void - Not a type, specifies absence of a type
Enums:
enum Category {Biography, Poetry, Fiction};//0,1,2
Arrays:
- Arrays work the same way as JS with some notable exceptions
- Accessed and used much like JS Arrays
- Declare as an array of "any" to store any type in the same array
Tuples:
- Kind of Arrays where we can specify the type for first few elements
- Additional elements can be any type from those previously specified
Functions in TS Vs JS:
- Types in TS, not supported in JavaScript
- Arrow functions in TS, introduced in ES2015
- Required and Optional parameters in TS, In JS all parameters are Optional
- Specify default parameters in TS, introduced in ES2015
- Rest parameters, introduced in ES 2015
- Overloading Support in TS, not supported in JS
Arrow Functions:
- Consice syntax for anonymous functions
- On it lefts takes paramters and right sode contians the body of the function Ex: let arr = strArray.filter(emp => emp.Name ==="Test1");
Function Types -- Like Delegates in C#:
- Combination of parameter types and return type
Optional and Defautl Parameters:
- In JS all parameters are optional, in TS we need to explicitly mention the param as optional
- We can mark the parameter as Optional just by adding "?" -> Same as C#
- Default parameters can be set to a literal value or an expression
Rest Parameters:
- Collects a group of parameters into a single array
- Denoted with an ellipsis prefix on last parameter
Function Overloading:
- Can have different types, but type definition can be Same
Interfaces:
One of TypeScript’s core principles is that type-checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.
Interfaces for funtion types:
Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties, interfaces are also capable of describing function types.
Extending Interfaces:
Interfaces can be extended from other interfaces
Classes:
-
Template for creating objects
-
Provides state storage and behavior
-
Encapsulates resusable functionality
-
Through Classes Typescript adds Object Oreiented features to JS
Constructors:
- Method named "constructor" - maximum of one per class
- We can use optional parameters to call different ways
- Executed by using a new keyword
Properties and Methods:
- Public properties
- Static Properties - Store data specofoc to the class not to the instances of the class
Access Modifiers:
- By default "Public" will be applied in TS
- Private - Only in the class
- Protected
Extending classes with Inheritence
Abstract classes
Class Expressions:
Another ES2015 featyre that TypeScript supports. Using class expressions we can omit the class name while creating the classes.