Class is a container, its role is javascript/typescript is to just encapsulate a code such as functions or variables into reusable containers that can be used thoughout the application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classCar{// Fields/*
variables that we can put in a class
*/// Constructor/*
used to initialize an object. You might pass some started data that the object needs at runtime
*/// Properties// Functions}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classCar{// Fieldsengine:string;// Constructorconstructor(engine:string){this.engine=engine;}// Shorthand wayclassCar{// instead of defining field explicitly. We can just use public keyword in front of the variable name and typescript will generate that field for us.constructor(publicengine:string){}}}
complex type
1
2
3
4
5
6
7
8
9
10
11
classEngine{constructor(publichorsePower:number,publicengineType:string){}}classCar{// Engine here is a complex type - using class inside a classprivate_engine:Engine;constructor(engine:Engine){}}
Casting types <>
1
2
3
4
// cast HTMLElement to HTMLTableElementvartable:HTMLTableElement=<HTMLTableElement>document.createElement('table');// document.createElement('table'); returns HTMLElement
typescript knows about these (HTMLTableElements or HTMLInputElement) by Type Definition Files
Type Definition Files
For working with DOM or other libraries, a Type Definition File(*.d.ts) is required.
Help in keep separation in our code. Use module keyword to declare a module. Prevent global namespace from getting polluted.
1
2
3
4
// Explicitly declare a modulemoduledataService{// code }
Internal vs External modules
| Internal Modules | External Modules |
| ———– | ———– |
| Namespace-like modules | Separately loadable modules |
| For grouping code | Exported modules can be imported into other modules |
| No need to import them | Need to import import model = require('/models');
| | Uses CommonJS or AMD conventions
| Sequence of loading files is important | Can be loaded in any sequence |
1. Named modules or internal modules
We need to export a class from a namespace so that it can be used outside.
module keyword has been replaced with namespace in TypeScript 1.5
```ts
namespace Shapes {
export class Rectangle {
constructor(public height: number, public width: number) {}
}
}
var rect = new Shapes.Rectangle(2,4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
This code gets converted to an `IIFE`, which is using the concept of module pattern or revealing module pattern.
```js
// equivalent JS
"use strict";
var Shapes;
(function (Shapes) {
var Rectangle = /** @class */ (function () {
function Rectangle(height, width) {
this.height = height;
this.width = width;
}
return Rectangle;
}());
Shapes.Rectangle = Rectangle;
})(Shapes || (Shapes = {}));
var rect = new Shapes.Rectangle(2, 4);
Extending named modules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespaceShapes{exportclassRectangle{constructor(publicheight:number,publicwidth:number){}}}varrect=newShapes.Rectangle(2,4);// just by using the same namenamespaceShapes{exportclassSquare{constructor(publicheight:number,publicwidth:number){}}}varsq=newShapes.Square(2,4);